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()); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 100 | name.append("NLSR"); |
| 101 | name.append("INFO"); |
| 102 | |
| 103 | NLSR_LOG_DEBUG("Setting interest filter for Hello interest: " << name); |
| 104 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 105 | getNlsrFace().setInterestFilter(name, |
Joao Pereira | 97473d4 | 2015-07-03 16:57:27 -0400 | [diff] [blame] | 106 | std::bind(&HelloProtocol::processInterest, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 107 | &m_helloProtocol, _1, _2), |
Joao Pereira | 97473d4 | 2015-07-03 16:57:27 -0400 | [diff] [blame] | 108 | std::bind(&Nlsr::onRegistrationSuccess, this, _1), |
| 109 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 110 | m_signingInfo, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 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 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 121 | getNlsrFace().setInterestFilter(name, |
Joao Pereira | 97473d4 | 2015-07-03 16:57:27 -0400 | [diff] [blame] | 122 | std::bind(&Lsdb::processInterest, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 123 | &m_nlsrLsdb, _1, _2), |
Joao Pereira | 97473d4 | 2015-07-03 16:57:27 -0400 | [diff] [blame] | 124 | std::bind(&Nlsr::onRegistrationSuccess, this, _1), |
| 125 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 126 | m_signingInfo, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 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 { |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 135 | m_dispatcher.addTopPrefix(topPrefix, false, m_signingInfo); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 136 | } |
| 137 | catch (const std::exception& e) { |
| 138 | NLSR_LOG_ERROR("Error setting top-level prefix in dispatcher: " << e.what() << "\n"); |
| 139 | } |
| 140 | } |
| 141 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 142 | void |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 143 | Nlsr::setStrategies() |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 144 | { |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame^] | 145 | NLSR_LOG_TRACE("in setStrategies"); |
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()); |
| 169 | uri.canonize([this, 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 | }, |
| 175 | [this, 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 | |
| 263 | m_nlsrLsdb.getSyncLogicHandler().createSyncSocket(m_confParam.getChronosyncPrefix()); |
Vince Lehman | c11cc20 | 2015-01-20 11:41:33 -0600 | [diff] [blame] | 264 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 265 | // Logging start |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 266 | m_confParam.writeLog(); |
| 267 | m_adjacencyList.writeLog(); |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 268 | NLSR_LOG_DEBUG(m_namePrefixList); |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 269 | // Logging end |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 270 | |
akmhoque | 443ad81 | 2014-07-29 10:26:56 -0500 | [diff] [blame] | 271 | initializeKey(); |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 272 | setStrategies(); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 273 | |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 274 | NLSR_LOG_DEBUG("Default NLSR identity: " << m_signingInfo.getSignerName()); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 275 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 276 | setInfoInterestFilter(); |
| 277 | setLsaInterestFilter(); |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 278 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 279 | // add top-level prefixes: router and localhost prefix |
| 280 | addDispatcherTopPrefix(m_confParam.getRouterPrefix()); |
| 281 | addDispatcherTopPrefix(LOCALHOST_PREFIX); |
| 282 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 283 | initializeFaces(std::bind(&Nlsr::processFaceDataset, this, _1), |
| 284 | std::bind(&Nlsr::onFaceDatasetFetchTimeout, this, _1, _2, 0)); |
| 285 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 286 | enableIncomingFaceIdIndication(); |
| 287 | |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 288 | // Set event intervals |
| 289 | setFirstHelloInterval(m_confParam.getFirstHelloInterval()); |
| 290 | m_nlsrLsdb.setAdjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval()); |
| 291 | m_routingTable.setRoutingCalcInterval(m_confParam.getRoutingCalcInterval()); |
| 292 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 293 | m_nlsrLsdb.buildAndInstallOwnNameLsa(); |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 294 | |
| 295 | // Install coordinate LSAs if using HR or dry-run HR. |
| 296 | if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) { |
| 297 | m_nlsrLsdb.buildAndInstallOwnCoordinateLsa(); |
| 298 | } |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 299 | |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 300 | registerKeyPrefix(); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 301 | registerLocalhostPrefix(); |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 302 | |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 303 | m_helloProtocol.scheduleInterest(m_firstHelloInterval); |
Vince Lehman | 0913112 | 2014-09-09 17:10:11 -0500 | [diff] [blame] | 304 | |
| 305 | // Need to set direct neighbors' costs to 0 for hyperbolic routing |
| 306 | if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) { |
| 307 | |
| 308 | std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList(); |
| 309 | |
| 310 | for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) { |
| 311 | it->setLinkCost(0); |
| 312 | } |
| 313 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | void |
akmhoque | 443ad81 | 2014-07-29 10:26:56 -0500 | [diff] [blame] | 317 | Nlsr::initializeKey() |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 318 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 319 | NLSR_LOG_DEBUG("Initializing Key ..."); |
| 320 | |
| 321 | ndn::Name nlsrInstanceName = m_confParam.getRouterPrefix(); |
| 322 | nlsrInstanceName.append("NLSR"); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 323 | |
Joao Pereira | 97473d4 | 2015-07-03 16:57:27 -0400 | [diff] [blame] | 324 | try { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 325 | m_keyChain.deleteIdentity(m_keyChain.getPib().getIdentity(nlsrInstanceName)); |
| 326 | } catch (const std::exception& e) { |
| 327 | NLSR_LOG_WARN(e.what()); |
| 328 | } |
| 329 | |
| 330 | auto nlsrInstanceIdentity = m_keyChain.createIdentity(nlsrInstanceName); |
| 331 | auto nlsrInstanceKey = nlsrInstanceIdentity.getDefaultKey(); |
| 332 | |
| 333 | ndn::security::v2::Certificate certificate; |
| 334 | |
| 335 | ndn::Name certificateName = nlsrInstanceKey.getName(); |
| 336 | certificateName.append("NA"); |
| 337 | certificateName.appendVersion(); |
| 338 | certificate.setName(certificateName); |
| 339 | |
| 340 | // set metainfo |
| 341 | certificate.setContentType(ndn::tlv::ContentType_Key); |
Ashlesh Gawande | 0d2c382 | 2018-01-24 17:17:15 -0600 | [diff] [blame] | 342 | certificate.setFreshnessPeriod(ndn::time::days(365)); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 343 | |
| 344 | // set content |
| 345 | certificate.setContent(nlsrInstanceKey.getPublicKey().data(), nlsrInstanceKey.getPublicKey().size()); |
| 346 | |
| 347 | // set signature-info |
| 348 | ndn::SignatureInfo signatureInfo; |
| 349 | signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(ndn::time::system_clock::TimePoint(), |
| 350 | ndn::time::system_clock::now() |
Ashlesh Gawande | 0d2c382 | 2018-01-24 17:17:15 -0600 | [diff] [blame] | 351 | + ndn::time::days(365))); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 352 | try { |
| 353 | m_keyChain.sign(certificate, |
| 354 | ndn::security::SigningInfo(m_keyChain.getPib().getIdentity(m_confParam.getRouterPrefix())) |
| 355 | .setSignatureInfo(signatureInfo)); |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 356 | } |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 357 | catch (const std::exception& e) { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 358 | NLSR_LOG_WARN("ERROR: Router's " << e.what() |
| 359 | << "NLSR is running without security." |
| 360 | << " If security is enabled NLSR will not converge."); |
| 361 | |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame^] | 362 | std::cerr << "Router's " << e.what() << ". NLSR is running without security " |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 363 | << "(Only for testing, should not be used in production.)" |
| 364 | << " If security is enabled NLSR will not converge." << std::endl; |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 365 | } |
akmhoque | 443ad81 | 2014-07-29 10:26:56 -0500 | [diff] [blame] | 366 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 367 | m_signingInfo = ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID, |
| 368 | nlsrInstanceName); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 369 | |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 370 | loadCertToPublish(certificate); |
| 371 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 372 | m_defaultCertName = certificate.getName(); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void |
| 376 | Nlsr::registerKeyPrefix() |
| 377 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 378 | // Start listening for the interest of this router's NLSR certificate |
| 379 | ndn::Name nlsrKeyPrefix = getConfParameter().getRouterPrefix(); |
| 380 | nlsrKeyPrefix.append("NLSR"); |
| 381 | nlsrKeyPrefix.append("KEY"); |
| 382 | |
| 383 | m_nlsrFace.setInterestFilter(nlsrKeyPrefix, |
Joao Pereira | 97473d4 | 2015-07-03 16:57:27 -0400 | [diff] [blame] | 384 | std::bind(&Nlsr::onKeyInterest, |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 385 | this, _1, _2), |
Joao Pereira | 97473d4 | 2015-07-03 16:57:27 -0400 | [diff] [blame] | 386 | std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1), |
| 387 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 388 | m_signingInfo, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 389 | ndn::nfd::ROUTE_FLAG_CAPTURE); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 390 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 391 | // Start listening for the interest of this router's certificate |
| 392 | ndn::Name routerKeyPrefix = getConfParameter().getRouterPrefix(); |
| 393 | routerKeyPrefix.append("KEY"); |
| 394 | |
| 395 | m_nlsrFace.setInterestFilter(routerKeyPrefix, |
| 396 | std::bind(&Nlsr::onKeyInterest, |
| 397 | this, _1, _2), |
| 398 | std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1), |
| 399 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 400 | m_signingInfo, |
| 401 | ndn::nfd::ROUTE_FLAG_CAPTURE); |
| 402 | |
| 403 | // Start listening for the interest of this router's operator's certificate |
| 404 | ndn::Name operatorKeyPrefix = getConfParameter().getNetwork(); |
| 405 | operatorKeyPrefix.append(getConfParameter().getSiteName()); |
| 406 | operatorKeyPrefix.append(std::string("%C1.Operator")); |
| 407 | |
| 408 | m_nlsrFace.setInterestFilter(operatorKeyPrefix, |
| 409 | std::bind(&Nlsr::onKeyInterest, |
| 410 | this, _1, _2), |
| 411 | std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1), |
| 412 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 413 | m_signingInfo, |
| 414 | ndn::nfd::ROUTE_FLAG_CAPTURE); |
| 415 | |
| 416 | // Start listening for the interest of this router's site's certificate |
| 417 | ndn::Name siteKeyPrefix = getConfParameter().getNetwork(); |
| 418 | siteKeyPrefix.append(getConfParameter().getSiteName()); |
| 419 | siteKeyPrefix.append("KEY"); |
| 420 | |
| 421 | m_nlsrFace.setInterestFilter(siteKeyPrefix, |
| 422 | std::bind(&Nlsr::onKeyInterest, |
| 423 | this, _1, _2), |
| 424 | std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1), |
| 425 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 426 | m_signingInfo, |
| 427 | ndn::nfd::ROUTE_FLAG_CAPTURE); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | void |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 431 | Nlsr::registerLocalhostPrefix() |
| 432 | { |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 433 | m_nlsrFace.registerPrefix(LOCALHOST_PREFIX, |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 434 | std::bind(&Nlsr::onRegistrationSuccess, this, _1), |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 435 | std::bind(&Nlsr::registrationFailed, this, _1)); |
| 436 | } |
| 437 | |
| 438 | void |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 439 | Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest) |
| 440 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 441 | NLSR_LOG_DEBUG("Got interest for certificate. Interest: " << interest.getName()); |
| 442 | |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 443 | const ndn::Name& interestName = interest.getName(); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 444 | const ndn::security::v2::Certificate* cert = getCertificate(interestName); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 445 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 446 | if (cert == nullptr) { |
| 447 | NLSR_LOG_DEBUG("Certificate is not found for: " << interest); |
dmcoomes | 9eaf3f4 | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 448 | return; // cert is not found |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 449 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 450 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 451 | m_nlsrFace.put(*cert); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | void |
| 455 | Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name) |
| 456 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 457 | NLSR_LOG_DEBUG("KEY prefix: " << name << " registration is successful."); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 458 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 459 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 460 | void |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 461 | Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification) |
akmhoque | e176515 | 2014-06-30 11:32:01 -0500 | [diff] [blame] | 462 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 463 | NLSR_LOG_TRACE("Nlsr::onFaceEventNotification called"); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 464 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 465 | switch (faceEventNotification.getKind()) { |
| 466 | case ndn::nfd::FACE_EVENT_DESTROYED: { |
| 467 | uint64_t faceId = faceEventNotification.getFaceId(); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 468 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 469 | auto adjacent = m_adjacencyList.findAdjacent(faceId); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 470 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 471 | if (adjacent != m_adjacencyList.end()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 472 | NLSR_LOG_DEBUG("Face to " << adjacent->getName() << " with face id: " << faceId << " destroyed"); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 473 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 474 | adjacent->setFaceId(0); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 475 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 476 | // Only trigger an Adjacency LSA build if this node is changing |
| 477 | // from ACTIVE to INACTIVE since this rebuild will effectively |
| 478 | // cancel the previous Adjacency LSA refresh event and schedule |
| 479 | // a new one further in the future. |
| 480 | // |
| 481 | // Continuously scheduling the refresh in the future will block |
| 482 | // the router from refreshing its Adjacency LSA. Since other |
| 483 | // routers' Name prefixes' expiration times are updated when |
| 484 | // this router refreshes its Adjacency LSA, the other routers' |
| 485 | // prefixes will expire and be removed from the RIB. |
| 486 | // |
| 487 | // This check is required to fix Bug #2733 for now. This check |
| 488 | // would be unnecessary to fix Bug #2733 when Issue #2732 is |
| 489 | // completed, but the check also helps with optimization so it |
| 490 | // can remain even when Issue #2732 is implemented. |
| 491 | if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) { |
| 492 | adjacent->setStatus(Adjacent::STATUS_INACTIVE); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 493 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 494 | // A new adjacency LSA cannot be built until the neighbor is marked INACTIVE and |
| 495 | // has met the HELLO retry threshold |
| 496 | adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber()); |
Vince Lehman | 199e9cf | 2015-04-07 13:22:16 -0500 | [diff] [blame] | 497 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 498 | if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) { |
| 499 | getRoutingTable().scheduleRoutingTableCalculation(*this); |
| 500 | } |
| 501 | else { |
| 502 | m_nlsrLsdb.scheduleAdjLsaBuild(); |
| 503 | } |
Nick Gordon | e8e03ac | 2016-07-07 14:24:38 -0500 | [diff] [blame] | 504 | } |
Vince Lehman | 199e9cf | 2015-04-07 13:22:16 -0500 | [diff] [blame] | 505 | } |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 506 | break; |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 507 | } |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 508 | case ndn::nfd::FACE_EVENT_CREATED: { |
| 509 | // Find the neighbor in our adjacency list |
Ashlesh Gawande | 0d2c382 | 2018-01-24 17:17:15 -0600 | [diff] [blame] | 510 | ndn::FaceUri faceUri; |
| 511 | try { |
| 512 | faceUri = ndn::FaceUri(faceEventNotification.getRemoteUri()); |
| 513 | } |
| 514 | catch (const std::exception& e) { |
| 515 | NLSR_LOG_WARN(e.what()); |
| 516 | return; |
| 517 | } |
| 518 | auto adjacent = m_adjacencyList.findAdjacent(faceUri); |
| 519 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 520 | // If we have a neighbor by that FaceUri and it has no FaceId, we |
| 521 | // have a match. |
| 522 | if (adjacent != m_adjacencyList.end()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 523 | NLSR_LOG_DEBUG("Face creation event matches neighbor: " << adjacent->getName() |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 524 | << ". New Face ID: " << faceEventNotification.getFaceId() |
| 525 | << ". Registering prefixes."); |
| 526 | adjacent->setFaceId(faceEventNotification.getFaceId()); |
| 527 | |
| 528 | registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max()); |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 529 | |
| 530 | if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) { |
| 531 | getRoutingTable().scheduleRoutingTableCalculation(*this); |
| 532 | } |
| 533 | else { |
| 534 | m_nlsrLsdb.scheduleAdjLsaBuild(); |
| 535 | } |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 536 | } |
| 537 | break; |
| 538 | } |
| 539 | default: |
| 540 | break; |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 541 | } |
akmhoque | e176515 | 2014-06-30 11:32:01 -0500 | [diff] [blame] | 542 | } |
| 543 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 544 | void |
| 545 | Nlsr::initializeFaces(const FetchDatasetCallback& onFetchSuccess, |
| 546 | const FetchDatasetTimeoutCallback& onFetchFailure) |
| 547 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 548 | NLSR_LOG_TRACE("Initializing Faces..."); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 549 | |
| 550 | m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure); |
| 551 | |
| 552 | } |
| 553 | |
| 554 | void |
| 555 | Nlsr::processFaceDataset(const std::vector<ndn::nfd::FaceStatus>& faces) |
| 556 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 557 | NLSR_LOG_DEBUG("Processing face dataset"); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 558 | |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 559 | // Iterate over each neighbor listed in nlsr.conf |
| 560 | for (auto& adjacent : m_adjacencyList.getAdjList()) { |
| 561 | |
| 562 | const std::string faceUriString = adjacent.getFaceUri().toString(); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 563 | // Check the list of FaceStatus objects we got for a match |
| 564 | for (const ndn::nfd::FaceStatus& faceStatus : faces) { |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 565 | // Set the adjacency FaceID if we find a URI match and it was |
| 566 | // previously unset. Change the boolean to true. |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 567 | if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 568 | NLSR_LOG_DEBUG("FaceUri: " << faceStatus.getRemoteUri() << |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 569 | " FaceId: "<< faceStatus.getFaceId()); |
| 570 | adjacent.setFaceId(faceStatus.getFaceId()); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 571 | // Register the prefixes for each neighbor |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 572 | this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max()); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | // If this adjacency has no information in this dataset, then one |
| 576 | // of two things is happening: 1. NFD is starting slowly and this |
| 577 | // Face wasn't ready yet, or 2. NFD is configured |
| 578 | // incorrectly and this Face isn't available. |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 579 | if (adjacent.getFaceId() == 0) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 580 | NLSR_LOG_WARN("The adjacency " << adjacent.getName() << |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 581 | " has no Face information in this dataset."); |
| 582 | } |
| 583 | } |
| 584 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 585 | scheduleDatasetFetch(); |
| 586 | } |
| 587 | |
| 588 | void |
| 589 | Nlsr::registerAdjacencyPrefixes(const Adjacent& adj, |
| 590 | const ndn::time::milliseconds& timeout) |
| 591 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 592 | ndn::FaceUri faceUri = adj.getFaceUri(); |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 593 | double linkCost = adj.getLinkCost(); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 594 | const ndn::Name& adjName = adj.getName(); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 595 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 596 | m_fib.registerPrefix(adjName, faceUri, linkCost, |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 597 | timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 598 | |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 599 | m_fib.registerPrefix(m_confParam.getChronosyncPrefix(), |
| 600 | faceUri, linkCost, timeout, |
| 601 | ndn::nfd::ROUTE_FLAG_CAPTURE, 0); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 602 | |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 603 | m_fib.registerPrefix(m_confParam.getLsaPrefix(), |
| 604 | faceUri, linkCost, timeout, |
| 605 | ndn::nfd::ROUTE_FLAG_CAPTURE, 0); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | void |
| 609 | Nlsr::onFaceDatasetFetchTimeout(uint32_t code, |
| 610 | const std::string& msg, |
| 611 | uint32_t nRetriesSoFar) |
| 612 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 613 | NLSR_LOG_DEBUG("onFaceDatasetFetchTimeout"); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 614 | // If we have exceeded the maximum attempt count, do not try again. |
| 615 | if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 616 | NLSR_LOG_DEBUG("Failed to fetch dataset: " << msg << ". Attempting retry #" << nRetriesSoFar); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 617 | m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset, |
| 618 | this, _1), |
| 619 | std::bind(&Nlsr::onFaceDatasetFetchTimeout, |
| 620 | this, _1, _2, nRetriesSoFar)); |
| 621 | } |
| 622 | else { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 623 | NLSR_LOG_ERROR("Failed to fetch dataset: " << msg << ". Exceeded limit of " << |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 624 | m_confParam.getFaceDatasetFetchTries() << ", so not trying again this time."); |
| 625 | // If we fail to fetch it, just do nothing until the next |
| 626 | // interval. Since this is a backup mechanism, we aren't as |
| 627 | // concerned with retrying. |
| 628 | scheduleDatasetFetch(); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | void |
| 633 | Nlsr::scheduleDatasetFetch() |
| 634 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 635 | NLSR_LOG_DEBUG("Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval()); |
| 636 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 637 | m_scheduler.scheduleEvent(m_confParam.getFaceDatasetFetchInterval(), |
| 638 | [this] { |
| 639 | this->initializeFaces( |
| 640 | [this] (const std::vector<ndn::nfd::FaceStatus>& faces) { |
| 641 | this->processFaceDataset(faces); |
| 642 | }, |
| 643 | [this] (uint32_t code, const std::string& msg) { |
| 644 | this->onFaceDatasetFetchTimeout(code, msg, 0); |
| 645 | }); |
| 646 | }); |
| 647 | } |
akmhoque | e176515 | 2014-06-30 11:32:01 -0500 | [diff] [blame] | 648 | |
| 649 | void |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 650 | Nlsr::enableIncomingFaceIdIndication() |
| 651 | { |
| 652 | NLSR_LOG_DEBUG("Enabling incoming face id indication for local face."); |
| 653 | |
| 654 | m_controller.start<ndn::nfd::FaceUpdateCommand>( |
| 655 | ndn::nfd::ControlParameters() |
| 656 | .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED, true), |
| 657 | bind(&Nlsr::onFaceIdIndicationSuccess, this, _1), |
| 658 | bind(&Nlsr::onFaceIdIndicationFailure, this, _1)); |
| 659 | } |
| 660 | |
| 661 | void |
| 662 | Nlsr::onFaceIdIndicationSuccess(const ndn::nfd::ControlParameters& cp) |
| 663 | { |
| 664 | NLSR_LOG_DEBUG("Successfully enabled incoming face id indication" |
| 665 | << "for face id " << cp.getFaceId()); |
| 666 | } |
| 667 | |
| 668 | void |
| 669 | Nlsr::onFaceIdIndicationFailure(const ndn::nfd::ControlResponse& cr) |
| 670 | { |
| 671 | std::ostringstream os; |
| 672 | os << "Failed to enable incoming face id indication feature: " << |
| 673 | "(code: " << cr.getCode() << ", reason: " << cr.getText() << ")"; |
| 674 | |
| 675 | NLSR_LOG_DEBUG(os.str()); |
| 676 | } |
| 677 | |
| 678 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 679 | Nlsr::startEventLoop() |
| 680 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 681 | m_nlsrFace.processEvents(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 682 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 683 | |
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame] | 684 | } // namespace nlsr |