akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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> |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 31 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 32 | #include <ndn-cxx/net/face-uri.hpp> |
| 33 | #include <ndn-cxx/signature.hpp> |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 34 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 35 | namespace nlsr { |
| 36 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 37 | INIT_LOGGER("nlsr"); |
| 38 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 39 | const ndn::Name Nlsr::LOCALHOST_PREFIX = ndn::Name("/localhost/nlsr"); |
| 40 | |
Laqin Fan | a4cf402 | 2017-01-03 18:57:35 +0000 | [diff] [blame] | 41 | 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] | 42 | : m_nlsrFace(face) |
| 43 | , m_scheduler(scheduler) |
Laqin Fan | a4cf402 | 2017-01-03 18:57:35 +0000 | [diff] [blame] | 44 | , m_keyChain(keyChain) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 45 | , m_confParam() |
| 46 | , m_adjacencyList() |
| 47 | , m_namePrefixList() |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 48 | , m_isDaemonProcess(false) |
| 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 | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 58 | , m_localhostDispatcher(m_nlsrFace, m_keyChain) |
| 59 | , m_routerNameDispatcher(m_nlsrFace, m_keyChain) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 60 | , m_lsdbDatasetHandler(m_nlsrLsdb, |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 61 | m_localhostDispatcher, |
| 62 | m_routerNameDispatcher, |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 63 | m_nlsrFace, |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 64 | m_keyChain) |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 65 | , m_helloProtocol(*this, scheduler) |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 66 | , m_validator(ndn::make_unique<ndn::security::v2::CertificateFetcherDirectFetch>(m_nlsrFace)) |
| 67 | , m_controller(m_nlsrFace, m_keyChain) |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 68 | , m_faceDatasetController(m_nlsrFace, m_keyChain) |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 69 | , m_prefixUpdateProcessor(m_localhostDispatcher, |
| 70 | m_nlsrFace, |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 71 | m_namePrefixList, |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 72 | m_nlsrLsdb) |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 73 | , m_nfdRibCommandProcessor(m_localhostDispatcher, |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 74 | m_namePrefixList, |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 75 | m_nlsrLsdb) |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 76 | , m_statsCollector(m_nlsrLsdb, m_helloProtocol) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 77 | , m_faceMonitor(m_nlsrFace) |
| 78 | , m_firstHelloInterval(FIRST_HELLO_INTERVAL_DEFAULT) |
| 79 | { |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 80 | m_faceMonitor.onNotification.connect(std::bind(&Nlsr::onFaceEventNotification, this, _1)); |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 81 | m_faceMonitor.start(); |
| 82 | } |
| 83 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 84 | void |
| 85 | Nlsr::registrationFailed(const ndn::Name& name) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 86 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 87 | NLSR_LOG_ERROR("ERROR: Failed to register prefix in local hub's daemon"); |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 88 | BOOST_THROW_EXCEPTION(Error("Error: Prefix registration failed")); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 89 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 90 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 91 | void |
| 92 | Nlsr::onRegistrationSuccess(const ndn::Name& name) |
| 93 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 94 | NLSR_LOG_DEBUG("Successfully registered prefix: " << name); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 98 | Nlsr::setInfoInterestFilter() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 99 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 100 | ndn::Name name(m_confParam.getRouterPrefix()); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 101 | name.append("NLSR"); |
| 102 | name.append("INFO"); |
| 103 | |
| 104 | NLSR_LOG_DEBUG("Setting interest filter for Hello interest: " << name); |
| 105 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 106 | getNlsrFace().setInterestFilter(name, |
Joao Pereira | 97473d4 | 2015-07-03 16:57:27 -0400 | [diff] [blame] | 107 | std::bind(&HelloProtocol::processInterest, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 108 | &m_helloProtocol, _1, _2), |
Joao Pereira | 97473d4 | 2015-07-03 16:57:27 -0400 | [diff] [blame] | 109 | std::bind(&Nlsr::onRegistrationSuccess, this, _1), |
| 110 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 111 | m_signingInfo, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 112 | ndn::nfd::ROUTE_FLAG_CAPTURE); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void |
| 116 | Nlsr::setLsaInterestFilter() |
| 117 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 118 | ndn::Name name = m_confParam.getLsaPrefix(); |
akmhoque | 50125a9 | 2014-06-30 08:54:17 -0500 | [diff] [blame] | 119 | name.append(m_confParam.getSiteName()); |
| 120 | name.append(m_confParam.getRouterName()); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 121 | |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 122 | NLSR_LOG_DEBUG("Setting interest filter for LsaPrefix: " << name); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 123 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 124 | getNlsrFace().setInterestFilter(name, |
Joao Pereira | 97473d4 | 2015-07-03 16:57:27 -0400 | [diff] [blame] | 125 | std::bind(&Lsdb::processInterest, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 126 | &m_nlsrLsdb, _1, _2), |
Joao Pereira | 97473d4 | 2015-07-03 16:57:27 -0400 | [diff] [blame] | 127 | std::bind(&Nlsr::onRegistrationSuccess, this, _1), |
| 128 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 129 | m_signingInfo, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 130 | ndn::nfd::ROUTE_FLAG_CAPTURE); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 133 | |
| 134 | void |
| 135 | Nlsr::addDispatcherTopPrefix(const ndn::Name& topPrefix) |
| 136 | { |
| 137 | try { |
| 138 | if (topPrefix.equals(m_confParam.getRouterPrefix())) { |
| 139 | m_routerNameDispatcher.addTopPrefix(topPrefix, false, m_signingInfo); |
| 140 | } |
| 141 | else { |
| 142 | m_localhostDispatcher.addTopPrefix(topPrefix, false, m_signingInfo); |
| 143 | } |
| 144 | } |
| 145 | catch (const std::exception& e) { |
| 146 | NLSR_LOG_ERROR("Error setting top-level prefix in dispatcher: " << e.what() << "\n"); |
| 147 | } |
| 148 | } |
| 149 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 150 | void |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 151 | Nlsr::setStrategies() |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 152 | { |
Vince Lehman | 53c0e3e | 2015-09-14 14:33:20 -0500 | [diff] [blame] | 153 | const std::string strategy("ndn:/localhost/nfd/strategy/multicast"); |
| 154 | |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 155 | m_fib.setStrategy(m_confParam.getLsaPrefix(), strategy, 0); |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 156 | m_fib.setStrategy(m_confParam.getChronosyncPrefix(), strategy, 0); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void |
akmhoque | 0494c25 | 2014-07-23 23:46:44 -0500 | [diff] [blame] | 160 | Nlsr::daemonize() |
| 161 | { |
| 162 | pid_t process_id = 0; |
| 163 | pid_t sid = 0; |
| 164 | process_id = fork(); |
| 165 | if (process_id < 0){ |
| 166 | std::cerr << "Daemonization failed!" << std::endl; |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 167 | BOOST_THROW_EXCEPTION(Error("Error: Daemonization process- fork failed!")); |
akmhoque | 0494c25 | 2014-07-23 23:46:44 -0500 | [diff] [blame] | 168 | } |
| 169 | if (process_id > 0) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 170 | NLSR_LOG_DEBUG("Process daemonized. Process id: " << process_id); |
akmhoque | 0494c25 | 2014-07-23 23:46:44 -0500 | [diff] [blame] | 171 | exit(0); |
| 172 | } |
| 173 | |
| 174 | umask(0); |
| 175 | sid = setsid(); |
| 176 | if(sid < 0) { |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 177 | BOOST_THROW_EXCEPTION(Error("Error: Daemonization process- setting id failed!")); |
akmhoque | 0494c25 | 2014-07-23 23:46:44 -0500 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | if (chdir("/") < 0) { |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 181 | BOOST_THROW_EXCEPTION(Error("Error: Daemonization process-chdir failed!")); |
akmhoque | 0494c25 | 2014-07-23 23:46:44 -0500 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | void |
Nick Gordon | 922714a | 2017-06-13 14:12:02 -0500 | [diff] [blame] | 186 | Nlsr::canonizeContinuation(std::list<Adjacent>::iterator iterator, |
| 187 | std::function<void(void)> finally) |
Nick Gordon | 9461afb | 2017-04-25 15:54:50 -0500 | [diff] [blame] | 188 | { |
Nick Gordon | 922714a | 2017-06-13 14:12:02 -0500 | [diff] [blame] | 189 | canonizeNeighborUris(iterator, [this, finally] (std::list<Adjacent>::iterator iterator) { |
| 190 | canonizeContinuation(iterator, finally); |
| 191 | }, |
| 192 | finally); |
Nick Gordon | 9461afb | 2017-04-25 15:54:50 -0500 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void |
| 196 | Nlsr::canonizeNeighborUris(std::list<Adjacent>::iterator currentNeighbor, |
Nick Gordon | 922714a | 2017-06-13 14:12:02 -0500 | [diff] [blame] | 197 | std::function<void(std::list<Adjacent>::iterator)> then, |
| 198 | std::function<void(void)> finally) |
Nick Gordon | 9461afb | 2017-04-25 15:54:50 -0500 | [diff] [blame] | 199 | { |
| 200 | if (currentNeighbor != m_adjacencyList.getAdjList().end()) { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 201 | ndn::FaceUri uri(currentNeighbor->getFaceUri()); |
| 202 | uri.canonize([this, then, currentNeighbor] (ndn::FaceUri canonicalUri) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 203 | NLSR_LOG_DEBUG("Canonized URI: " << currentNeighbor->getFaceUri() |
Nick Gordon | 9461afb | 2017-04-25 15:54:50 -0500 | [diff] [blame] | 204 | << " to: " << canonicalUri); |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 205 | currentNeighbor->setFaceUri(canonicalUri); |
Nick Gordon | 9461afb | 2017-04-25 15:54:50 -0500 | [diff] [blame] | 206 | then(std::next(currentNeighbor)); |
| 207 | }, |
| 208 | [this, then, currentNeighbor] (const std::string& reason) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 209 | NLSR_LOG_ERROR("Could not canonize URI: " << currentNeighbor->getFaceUri() |
Nick Gordon | 9461afb | 2017-04-25 15:54:50 -0500 | [diff] [blame] | 210 | << " because: " << reason); |
| 211 | then(std::next(currentNeighbor)); |
| 212 | }, |
| 213 | m_nlsrFace.getIoService(), |
| 214 | TIME_ALLOWED_FOR_CANONIZATION); |
| 215 | } |
Nick Gordon | 922714a | 2017-06-13 14:12:02 -0500 | [diff] [blame] | 216 | // We have finished canonizing all neighbors, so call finally() |
Nick Gordon | 9461afb | 2017-04-25 15:54:50 -0500 | [diff] [blame] | 217 | else { |
Nick Gordon | 922714a | 2017-06-13 14:12:02 -0500 | [diff] [blame] | 218 | finally(); |
Nick Gordon | 9461afb | 2017-04-25 15:54:50 -0500 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 222 | |
| 223 | void |
| 224 | Nlsr::loadCertToPublish(const ndn::security::v2::Certificate& certificate) |
| 225 | { |
| 226 | m_certStore.insert(certificate); |
| 227 | m_validator.loadAnchor("Authoritative-Certificate", |
| 228 | ndn::security::v2::Certificate(certificate)); |
| 229 | m_prefixUpdateProcessor.getValidator(). |
| 230 | loadAnchor("Authoritative-Certificate", |
| 231 | ndn::security::v2::Certificate(certificate)); |
| 232 | } |
| 233 | |
Nick Gordon | 9461afb | 2017-04-25 15:54:50 -0500 | [diff] [blame] | 234 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 235 | Nlsr::initialize() |
| 236 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 237 | NLSR_LOG_DEBUG("Initializing Nlsr"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 238 | m_confParam.buildRouterPrefix(); |
Muktadir R Chowdhury | 3ac0728 | 2016-06-17 16:30:29 -0500 | [diff] [blame] | 239 | m_lsdbDatasetHandler.setRouterNameCommandPrefix(m_confParam.getRouterPrefix()); |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 240 | m_nlsrLsdb.setLsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime())); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 241 | m_nlsrLsdb.setThisRouterPrefix(m_confParam.getRouterPrefix().toUri()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 242 | m_fib.setEntryRefreshTime(2 * m_confParam.getLsaRefreshTime()); |
Vince Lehman | c11cc20 | 2015-01-20 11:41:33 -0600 | [diff] [blame] | 243 | |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 244 | m_nlsrLsdb.getSequencingManager().setSeqFileDirectory(m_confParam.getSeqFileDir()); |
| 245 | m_nlsrLsdb.getSequencingManager().initiateSeqNoFromFile(m_confParam.getHyperbolicState()); |
| 246 | |
| 247 | m_nlsrLsdb.getSyncLogicHandler().createSyncSocket(m_confParam.getChronosyncPrefix()); |
Vince Lehman | c11cc20 | 2015-01-20 11:41:33 -0600 | [diff] [blame] | 248 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 249 | // Logging start |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 250 | m_confParam.writeLog(); |
| 251 | m_adjacencyList.writeLog(); |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 252 | NLSR_LOG_DEBUG(m_namePrefixList); |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 253 | // Logging end |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 254 | |
akmhoque | 443ad81 | 2014-07-29 10:26:56 -0500 | [diff] [blame] | 255 | initializeKey(); |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 256 | setStrategies(); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 257 | |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 258 | NLSR_LOG_DEBUG("Default NLSR identity: " << m_signingInfo.getSignerName()); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 259 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 260 | setInfoInterestFilter(); |
| 261 | setLsaInterestFilter(); |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 262 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 263 | // add top-level prefixes: router and localhost prefix |
| 264 | addDispatcherTopPrefix(m_confParam.getRouterPrefix()); |
| 265 | addDispatcherTopPrefix(LOCALHOST_PREFIX); |
| 266 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 267 | initializeFaces(std::bind(&Nlsr::processFaceDataset, this, _1), |
| 268 | std::bind(&Nlsr::onFaceDatasetFetchTimeout, this, _1, _2, 0)); |
| 269 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 270 | enableIncomingFaceIdIndication(); |
| 271 | |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 272 | // Set event intervals |
| 273 | setFirstHelloInterval(m_confParam.getFirstHelloInterval()); |
| 274 | m_nlsrLsdb.setAdjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval()); |
| 275 | m_routingTable.setRoutingCalcInterval(m_confParam.getRoutingCalcInterval()); |
| 276 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 277 | m_nlsrLsdb.buildAndInstallOwnNameLsa(); |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 278 | |
| 279 | // Install coordinate LSAs if using HR or dry-run HR. |
| 280 | if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) { |
| 281 | m_nlsrLsdb.buildAndInstallOwnCoordinateLsa(); |
| 282 | } |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 283 | |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 284 | registerKeyPrefix(); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 285 | registerLocalhostPrefix(); |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 286 | |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 287 | m_helloProtocol.scheduleInterest(m_firstHelloInterval); |
Vince Lehman | 0913112 | 2014-09-09 17:10:11 -0500 | [diff] [blame] | 288 | |
| 289 | // Need to set direct neighbors' costs to 0 for hyperbolic routing |
| 290 | if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) { |
| 291 | |
| 292 | std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList(); |
| 293 | |
| 294 | for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) { |
| 295 | it->setLinkCost(0); |
| 296 | } |
| 297 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void |
akmhoque | 443ad81 | 2014-07-29 10:26:56 -0500 | [diff] [blame] | 301 | Nlsr::initializeKey() |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 302 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 303 | NLSR_LOG_DEBUG("Initializing Key ..."); |
| 304 | |
| 305 | ndn::Name nlsrInstanceName = m_confParam.getRouterPrefix(); |
| 306 | nlsrInstanceName.append("NLSR"); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 307 | |
Joao Pereira | 97473d4 | 2015-07-03 16:57:27 -0400 | [diff] [blame] | 308 | try { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 309 | m_keyChain.deleteIdentity(m_keyChain.getPib().getIdentity(nlsrInstanceName)); |
| 310 | } catch (const std::exception& e) { |
| 311 | NLSR_LOG_WARN(e.what()); |
| 312 | } |
| 313 | |
| 314 | auto nlsrInstanceIdentity = m_keyChain.createIdentity(nlsrInstanceName); |
| 315 | auto nlsrInstanceKey = nlsrInstanceIdentity.getDefaultKey(); |
| 316 | |
| 317 | ndn::security::v2::Certificate certificate; |
| 318 | |
| 319 | ndn::Name certificateName = nlsrInstanceKey.getName(); |
| 320 | certificateName.append("NA"); |
| 321 | certificateName.appendVersion(); |
| 322 | certificate.setName(certificateName); |
| 323 | |
| 324 | // set metainfo |
| 325 | certificate.setContentType(ndn::tlv::ContentType_Key); |
| 326 | certificate.setFreshnessPeriod(ndn::time::days(7300)); |
| 327 | |
| 328 | // set content |
| 329 | certificate.setContent(nlsrInstanceKey.getPublicKey().data(), nlsrInstanceKey.getPublicKey().size()); |
| 330 | |
| 331 | // set signature-info |
| 332 | ndn::SignatureInfo signatureInfo; |
| 333 | signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(ndn::time::system_clock::TimePoint(), |
| 334 | ndn::time::system_clock::now() |
| 335 | + ndn::time::days(20 * 365))); |
| 336 | try { |
| 337 | m_keyChain.sign(certificate, |
| 338 | ndn::security::SigningInfo(m_keyChain.getPib().getIdentity(m_confParam.getRouterPrefix())) |
| 339 | .setSignatureInfo(signatureInfo)); |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 340 | } |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 341 | catch (const std::exception& e) { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 342 | NLSR_LOG_WARN("ERROR: Router's " << e.what() |
| 343 | << "NLSR is running without security." |
| 344 | << " If security is enabled NLSR will not converge."); |
| 345 | |
| 346 | std::cerr << "Router's " << e.what() << "NLSR is running without security " |
| 347 | << "(Only for testing, should not be used in production.)" |
| 348 | << " If security is enabled NLSR will not converge." << std::endl; |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 349 | } |
akmhoque | 443ad81 | 2014-07-29 10:26:56 -0500 | [diff] [blame] | 350 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 351 | m_signingInfo = ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID, |
| 352 | nlsrInstanceName); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 353 | |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 354 | loadCertToPublish(certificate); |
| 355 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 356 | m_defaultCertName = certificate.getName(); |
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 |
| 363 | ndn::Name nlsrKeyPrefix = getConfParameter().getRouterPrefix(); |
| 364 | nlsrKeyPrefix.append("NLSR"); |
| 365 | nlsrKeyPrefix.append("KEY"); |
| 366 | |
| 367 | m_nlsrFace.setInterestFilter(nlsrKeyPrefix, |
Joao Pereira | 97473d4 | 2015-07-03 16:57:27 -0400 | [diff] [blame] | 368 | std::bind(&Nlsr::onKeyInterest, |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 369 | this, _1, _2), |
Joao Pereira | 97473d4 | 2015-07-03 16:57:27 -0400 | [diff] [blame] | 370 | std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1), |
| 371 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 372 | m_signingInfo, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 373 | ndn::nfd::ROUTE_FLAG_CAPTURE); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 374 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 375 | // Start listening for the interest of this router's certificate |
| 376 | ndn::Name routerKeyPrefix = getConfParameter().getRouterPrefix(); |
| 377 | routerKeyPrefix.append("KEY"); |
| 378 | |
| 379 | m_nlsrFace.setInterestFilter(routerKeyPrefix, |
| 380 | std::bind(&Nlsr::onKeyInterest, |
| 381 | this, _1, _2), |
| 382 | std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1), |
| 383 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 384 | m_signingInfo, |
| 385 | ndn::nfd::ROUTE_FLAG_CAPTURE); |
| 386 | |
| 387 | // Start listening for the interest of this router's operator's certificate |
| 388 | ndn::Name operatorKeyPrefix = getConfParameter().getNetwork(); |
| 389 | operatorKeyPrefix.append(getConfParameter().getSiteName()); |
| 390 | operatorKeyPrefix.append(std::string("%C1.Operator")); |
| 391 | |
| 392 | m_nlsrFace.setInterestFilter(operatorKeyPrefix, |
| 393 | std::bind(&Nlsr::onKeyInterest, |
| 394 | this, _1, _2), |
| 395 | std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1), |
| 396 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 397 | m_signingInfo, |
| 398 | ndn::nfd::ROUTE_FLAG_CAPTURE); |
| 399 | |
| 400 | // Start listening for the interest of this router's site's certificate |
| 401 | ndn::Name siteKeyPrefix = getConfParameter().getNetwork(); |
| 402 | siteKeyPrefix.append(getConfParameter().getSiteName()); |
| 403 | siteKeyPrefix.append("KEY"); |
| 404 | |
| 405 | m_nlsrFace.setInterestFilter(siteKeyPrefix, |
| 406 | std::bind(&Nlsr::onKeyInterest, |
| 407 | this, _1, _2), |
| 408 | std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1), |
| 409 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 410 | m_signingInfo, |
| 411 | ndn::nfd::ROUTE_FLAG_CAPTURE); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | void |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 415 | Nlsr::registerLocalhostPrefix() |
| 416 | { |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 417 | m_nlsrFace.registerPrefix(LOCALHOST_PREFIX, |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 418 | std::bind(&Nlsr::onRegistrationSuccess, this, _1), |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 419 | std::bind(&Nlsr::registrationFailed, this, _1)); |
| 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 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 435 | m_nlsrFace.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) { |
| 483 | getRoutingTable().scheduleRoutingTableCalculation(*this); |
| 484 | } |
| 485 | else { |
| 486 | m_nlsrLsdb.scheduleAdjLsaBuild(); |
| 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 |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 494 | auto adjacent = m_adjacencyList.findAdjacent( |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 495 | ndn::FaceUri(faceEventNotification.getRemoteUri())); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 496 | // If we have a neighbor by that FaceUri and it has no FaceId, we |
| 497 | // have a match. |
| 498 | if (adjacent != m_adjacencyList.end()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 499 | NLSR_LOG_DEBUG("Face creation event matches neighbor: " << adjacent->getName() |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 500 | << ". New Face ID: " << faceEventNotification.getFaceId() |
| 501 | << ". Registering prefixes."); |
| 502 | adjacent->setFaceId(faceEventNotification.getFaceId()); |
| 503 | |
| 504 | registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max()); |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 505 | |
| 506 | if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) { |
| 507 | getRoutingTable().scheduleRoutingTableCalculation(*this); |
| 508 | } |
| 509 | else { |
| 510 | m_nlsrLsdb.scheduleAdjLsaBuild(); |
| 511 | } |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 512 | } |
| 513 | break; |
| 514 | } |
| 515 | default: |
| 516 | break; |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 517 | } |
akmhoque | e176515 | 2014-06-30 11:32:01 -0500 | [diff] [blame] | 518 | } |
| 519 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 520 | void |
| 521 | Nlsr::initializeFaces(const FetchDatasetCallback& onFetchSuccess, |
| 522 | const FetchDatasetTimeoutCallback& onFetchFailure) |
| 523 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 524 | NLSR_LOG_TRACE("Initializing Faces..."); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 525 | |
| 526 | m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure); |
| 527 | |
| 528 | } |
| 529 | |
| 530 | void |
| 531 | Nlsr::processFaceDataset(const std::vector<ndn::nfd::FaceStatus>& faces) |
| 532 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 533 | NLSR_LOG_DEBUG("Processing face dataset"); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 534 | |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 535 | // Iterate over each neighbor listed in nlsr.conf |
| 536 | for (auto& adjacent : m_adjacencyList.getAdjList()) { |
| 537 | |
| 538 | const std::string faceUriString = adjacent.getFaceUri().toString(); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 539 | // Check the list of FaceStatus objects we got for a match |
| 540 | for (const ndn::nfd::FaceStatus& faceStatus : faces) { |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 541 | // Set the adjacency FaceID if we find a URI match and it was |
| 542 | // previously unset. Change the boolean to true. |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 543 | if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 544 | NLSR_LOG_DEBUG("FaceUri: " << faceStatus.getRemoteUri() << |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 545 | " FaceId: "<< faceStatus.getFaceId()); |
| 546 | adjacent.setFaceId(faceStatus.getFaceId()); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 547 | // Register the prefixes for each neighbor |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 548 | this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max()); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | // If this adjacency has no information in this dataset, then one |
| 552 | // of two things is happening: 1. NFD is starting slowly and this |
| 553 | // Face wasn't ready yet, or 2. NFD is configured |
| 554 | // incorrectly and this Face isn't available. |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 555 | if (adjacent.getFaceId() == 0) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 556 | NLSR_LOG_WARN("The adjacency " << adjacent.getName() << |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 557 | " has no Face information in this dataset."); |
| 558 | } |
| 559 | } |
| 560 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 561 | scheduleDatasetFetch(); |
| 562 | } |
| 563 | |
| 564 | void |
| 565 | Nlsr::registerAdjacencyPrefixes(const Adjacent& adj, |
| 566 | const ndn::time::milliseconds& timeout) |
| 567 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 568 | ndn::FaceUri faceUri = adj.getFaceUri(); |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 569 | double linkCost = adj.getLinkCost(); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 570 | const ndn::Name& adjName = adj.getName(); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 571 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 572 | m_fib.registerPrefix(adjName, faceUri, linkCost, |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 573 | timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 574 | |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 575 | m_fib.registerPrefix(m_confParam.getChronosyncPrefix(), |
| 576 | faceUri, linkCost, timeout, |
| 577 | ndn::nfd::ROUTE_FLAG_CAPTURE, 0); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 578 | |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 579 | m_fib.registerPrefix(m_confParam.getLsaPrefix(), |
| 580 | faceUri, linkCost, timeout, |
| 581 | ndn::nfd::ROUTE_FLAG_CAPTURE, 0); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | void |
| 585 | Nlsr::onFaceDatasetFetchTimeout(uint32_t code, |
| 586 | const std::string& msg, |
| 587 | uint32_t nRetriesSoFar) |
| 588 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 589 | NLSR_LOG_DEBUG("onFaceDatasetFetchTimeout"); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 590 | // If we have exceeded the maximum attempt count, do not try again. |
| 591 | if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 592 | NLSR_LOG_DEBUG("Failed to fetch dataset: " << msg << ". Attempting retry #" << nRetriesSoFar); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 593 | m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset, |
| 594 | this, _1), |
| 595 | std::bind(&Nlsr::onFaceDatasetFetchTimeout, |
| 596 | this, _1, _2, nRetriesSoFar)); |
| 597 | } |
| 598 | else { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 599 | NLSR_LOG_ERROR("Failed to fetch dataset: " << msg << ". Exceeded limit of " << |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 600 | m_confParam.getFaceDatasetFetchTries() << ", so not trying again this time."); |
| 601 | // If we fail to fetch it, just do nothing until the next |
| 602 | // interval. Since this is a backup mechanism, we aren't as |
| 603 | // concerned with retrying. |
| 604 | scheduleDatasetFetch(); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | void |
| 609 | Nlsr::scheduleDatasetFetch() |
| 610 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 611 | NLSR_LOG_DEBUG("Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval()); |
| 612 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 613 | m_scheduler.scheduleEvent(m_confParam.getFaceDatasetFetchInterval(), |
| 614 | [this] { |
| 615 | this->initializeFaces( |
| 616 | [this] (const std::vector<ndn::nfd::FaceStatus>& faces) { |
| 617 | this->processFaceDataset(faces); |
| 618 | }, |
| 619 | [this] (uint32_t code, const std::string& msg) { |
| 620 | this->onFaceDatasetFetchTimeout(code, msg, 0); |
| 621 | }); |
| 622 | }); |
| 623 | } |
akmhoque | e176515 | 2014-06-30 11:32:01 -0500 | [diff] [blame] | 624 | |
| 625 | void |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 626 | Nlsr::enableIncomingFaceIdIndication() |
| 627 | { |
| 628 | NLSR_LOG_DEBUG("Enabling incoming face id indication for local face."); |
| 629 | |
| 630 | m_controller.start<ndn::nfd::FaceUpdateCommand>( |
| 631 | ndn::nfd::ControlParameters() |
| 632 | .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED, true), |
| 633 | bind(&Nlsr::onFaceIdIndicationSuccess, this, _1), |
| 634 | bind(&Nlsr::onFaceIdIndicationFailure, this, _1)); |
| 635 | } |
| 636 | |
| 637 | void |
| 638 | Nlsr::onFaceIdIndicationSuccess(const ndn::nfd::ControlParameters& cp) |
| 639 | { |
| 640 | NLSR_LOG_DEBUG("Successfully enabled incoming face id indication" |
| 641 | << "for face id " << cp.getFaceId()); |
| 642 | } |
| 643 | |
| 644 | void |
| 645 | Nlsr::onFaceIdIndicationFailure(const ndn::nfd::ControlResponse& cr) |
| 646 | { |
| 647 | std::ostringstream os; |
| 648 | os << "Failed to enable incoming face id indication feature: " << |
| 649 | "(code: " << cr.getCode() << ", reason: " << cr.getText() << ")"; |
| 650 | |
| 651 | NLSR_LOG_DEBUG(os.str()); |
| 652 | } |
| 653 | |
| 654 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 655 | Nlsr::startEventLoop() |
| 656 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 657 | m_nlsrFace.processEvents(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 658 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 659 | |
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame] | 660 | } // namespace nlsr |