Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 2 | /* |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, The University of Memphis, |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
| 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/>. |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 20 | */ |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 21 | |
| 22 | #include "nlsrc.hpp" |
| 23 | |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 24 | #include "config.hpp" |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 25 | #include "version.hpp" |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 26 | #include "src/publisher/dataset-interest-handler.hpp" |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 27 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 28 | #include <ndn-cxx/data.hpp> |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 29 | #include <ndn-cxx/encoding/block.hpp> |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 30 | #include <ndn-cxx/face.hpp> |
| 31 | #include <ndn-cxx/interest.hpp> |
Junxiao Shi | 3e5120c | 2016-09-10 16:58:34 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/mgmt/nfd/control-parameters.hpp> |
| 33 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
Davide Pesavento | 6184c20 | 2021-05-17 02:28:03 -0400 | [diff] [blame] | 34 | #include <ndn-cxx/security/interest-signer.hpp> |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 35 | #include <ndn-cxx/security/key-chain.hpp> |
| 36 | #include <ndn-cxx/security/signing-helpers.hpp> |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 37 | #include <ndn-cxx/security/validator-config.hpp> |
| 38 | #include <ndn-cxx/security/validator-null.hpp> |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 39 | #include <ndn-cxx/util/segment-fetcher.hpp> |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 40 | |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 41 | #include <boost/algorithm/string/replace.hpp> |
| 42 | #include <boost/property_tree/info_parser.hpp> |
| 43 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 44 | #include <iostream> |
| 45 | |
| 46 | namespace nlsrc { |
| 47 | |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 48 | const ndn::Name LOCALHOST_PREFIX("/localhost"); |
| 49 | const ndn::PartialName LSDB_SUFFIX("nlsr/lsdb"); |
| 50 | const ndn::PartialName NAME_UPDATE_SUFFIX("nlsr/prefix-update"); |
| 51 | const ndn::PartialName RT_SUFFIX("nlsr/routing-table"); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 52 | |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 53 | const uint32_t ERROR_CODE_TIMEOUT = 10060; |
| 54 | const uint32_t RESPONSE_CODE_SUCCESS = 200; |
| 55 | const uint32_t RESPONSE_CODE_SAVE_OR_DELETE = 205; |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 56 | |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 57 | Nlsrc::Nlsrc(std::string programName, ndn::Face& face) |
| 58 | : m_programName(std::move(programName)) |
| 59 | , m_routerPrefix(LOCALHOST_PREFIX) |
| 60 | , m_face(face) |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 61 | { |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 62 | disableValidator(); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 66 | Nlsrc::printUsage() const |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 67 | { |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 68 | std::string help(R"EOT(Usage: |
| 69 | @NLSRC@ [-h | -V] |
| 70 | @NLSRC@ [-R <router prefix> [-c <nlsr.conf path> | -k]] COMMAND [<Command Options>] |
| 71 | -h print usage and exit |
| 72 | -V print version and exit |
| 73 | -R target a remote NLSR instance |
| 74 | -c verify response with nlsr.conf security.validator policy |
| 75 | -k do not verify response (insecure) |
| 76 | |
| 77 | COMMAND can be one of the following: |
| 78 | lsdb |
| 79 | display NLSR lsdb status |
| 80 | routing |
| 81 | display routing table status |
| 82 | status |
| 83 | display all NLSR status (lsdb & routingtable) |
| 84 | advertise <name> |
| 85 | advertise a name prefix through NLSR |
| 86 | advertise <name> save |
| 87 | advertise and save the name prefix to the conf file |
| 88 | withdraw <name> |
| 89 | remove a name prefix advertised through NLSR |
| 90 | withdraw <name> delete |
| 91 | withdraw and delete the name prefix from the conf file |
| 92 | )EOT"); |
| 93 | boost::algorithm::replace_all(help, "@NLSRC@", m_programName); |
| 94 | std::cout << help; |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | Nlsrc::setRouterPrefix(ndn::Name prefix) |
| 99 | { |
| 100 | m_routerPrefix = std::move(prefix); |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | Nlsrc::disableValidator() |
| 105 | { |
| 106 | m_validator.reset(new ndn::security::ValidatorNull()); |
| 107 | } |
| 108 | |
| 109 | bool |
| 110 | Nlsrc::enableValidator(const std::string& filename) |
| 111 | { |
| 112 | using namespace boost::property_tree; |
| 113 | ptree validatorConfig; |
| 114 | try { |
| 115 | ptree config; |
| 116 | read_info(filename, config); |
| 117 | validatorConfig = config.get_child("security.validator"); |
| 118 | } |
| 119 | catch (const ptree_error& e) { |
| 120 | std::cerr << "Failed to parse configuration file '" << filename |
| 121 | << "': " << e.what() << std::endl; |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | auto validator = std::make_unique<ndn::security::ValidatorConfig>(m_face); |
| 126 | try { |
| 127 | validator->load(validatorConfig, filename); |
| 128 | } |
| 129 | catch (const ndn::security::validator_config::Error& e) { |
| 130 | std::cerr << "Failed to load validator config from '" << filename |
| 131 | << "' security.validator section: " << e.what() << std::endl; |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | m_validator = std::move(validator); |
| 136 | return true; |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 140 | Nlsrc::getStatus(const std::string& command) |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 141 | { |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 142 | if (command == "lsdb") { |
| 143 | m_fetchSteps.push_back(std::bind(&Nlsrc::fetchAdjacencyLsas, this)); |
| 144 | m_fetchSteps.push_back(std::bind(&Nlsrc::fetchCoordinateLsas, this)); |
| 145 | m_fetchSteps.push_back(std::bind(&Nlsrc::fetchNameLsas, this)); |
| 146 | m_fetchSteps.push_back(std::bind(&Nlsrc::printLsdb, this)); |
| 147 | } |
| 148 | else if (command == "routing") { |
| 149 | m_fetchSteps.push_back(std::bind(&Nlsrc::fetchRtables, this)); |
| 150 | m_fetchSteps.push_back(std::bind(&Nlsrc::printRT, this)); |
| 151 | } |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 152 | else if (command == "status") { |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 153 | m_fetchSteps.push_back(std::bind(&Nlsrc::fetchAdjacencyLsas, this)); |
| 154 | m_fetchSteps.push_back(std::bind(&Nlsrc::fetchCoordinateLsas, this)); |
| 155 | m_fetchSteps.push_back(std::bind(&Nlsrc::fetchNameLsas, this)); |
| 156 | m_fetchSteps.push_back(std::bind(&Nlsrc::fetchRtables, this)); |
| 157 | m_fetchSteps.push_back(std::bind(&Nlsrc::printAll, this)); |
| 158 | } |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 159 | runNextStep(); |
| 160 | } |
| 161 | |
| 162 | bool |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 163 | Nlsrc::dispatch(ndn::span<std::string> subcommand) |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 164 | { |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 165 | if (subcommand.size() == 0) { |
| 166 | return false; |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 167 | } |
| 168 | |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 169 | if (subcommand[0] == "advertise") { |
| 170 | switch (subcommand.size()) { |
| 171 | case 2: |
| 172 | advertiseName(subcommand[1], false); |
| 173 | return true; |
| 174 | case 3: |
| 175 | if (subcommand[2] != "save") { |
| 176 | return false; |
| 177 | } |
| 178 | advertiseName(subcommand[1], true); |
| 179 | return true; |
| 180 | } |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | if (subcommand[0] == "withdraw") { |
| 185 | switch (subcommand.size()) { |
| 186 | case 2: |
| 187 | withdrawName(subcommand[1], false); |
| 188 | return true; |
| 189 | case 3: |
| 190 | if (subcommand[2] != "delete") { |
| 191 | return false; |
| 192 | } |
| 193 | withdrawName(subcommand[1], true); |
| 194 | return true; |
| 195 | } |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | if (subcommand[0] == "lsdb" || subcommand[0] == "routing" || subcommand[0] == "status") { |
| 200 | if (subcommand.size() != 1) { |
| 201 | return false; |
| 202 | } |
| 203 | getStatus(subcommand[0]); |
| 204 | return true; |
| 205 | } |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 206 | return false; |
| 207 | } |
| 208 | |
| 209 | void |
| 210 | Nlsrc::runNextStep() |
| 211 | { |
| 212 | if (m_fetchSteps.empty()) { |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | std::function<void()> nextStep = m_fetchSteps.front(); |
| 217 | m_fetchSteps.pop_front(); |
| 218 | |
| 219 | nextStep(); |
| 220 | } |
| 221 | |
| 222 | void |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 223 | Nlsrc::advertiseName(ndn::Name name, bool wantSave) |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 224 | { |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 225 | std::string info = (wantSave ? "(Save: " : "(Advertise: ") + name.toUri() + ")"; |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 226 | ndn::Name::Component verb("advertise"); |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 227 | sendNamePrefixUpdate(name, verb, info, wantSave); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 231 | Nlsrc::withdrawName(ndn::Name name, bool wantDelete) |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 232 | { |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 233 | std::string info = (wantDelete ? "(Delete: " : "(Withdraw: ") + name.toUri() + ")"; |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 234 | ndn::Name::Component verb("withdraw"); |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 235 | sendNamePrefixUpdate(name, verb, info, wantDelete); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void |
| 239 | Nlsrc::sendNamePrefixUpdate(const ndn::Name& name, |
| 240 | const ndn::Name::Component& verb, |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 241 | const std::string& info, |
| 242 | bool flag) |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 243 | { |
| 244 | ndn::nfd::ControlParameters parameters; |
| 245 | parameters.setName(name); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 246 | if (flag) { |
| 247 | parameters.setFlags(1); |
| 248 | } |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 249 | |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 250 | auto paramWire = parameters.wireEncode(); |
| 251 | ndn::Name commandName = m_routerPrefix; |
| 252 | commandName.append(NAME_UPDATE_SUFFIX); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 253 | commandName.append(verb); |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 254 | commandName.append(paramWire.begin(), paramWire.end()); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 255 | |
Davide Pesavento | 6184c20 | 2021-05-17 02:28:03 -0400 | [diff] [blame] | 256 | ndn::security::InterestSigner signer(m_keyChain); |
| 257 | auto commandInterest = signer.makeCommandInterest(commandName, |
| 258 | ndn::security::signingByIdentity(m_keyChain.getPib().getDefaultIdentity())); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 259 | commandInterest.setMustBeFresh(true); |
| 260 | |
| 261 | m_face.expressInterest(commandInterest, |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 262 | std::bind(&Nlsrc::onControlResponse, this, info, _2), |
Alexander Afanasyev | 1de901f | 2017-03-09 12:43:57 -0800 | [diff] [blame] | 263 | std::bind(&Nlsrc::onTimeout, this, ERROR_CODE_TIMEOUT, "Nack"), |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 264 | std::bind(&Nlsrc::onTimeout, this, ERROR_CODE_TIMEOUT, "Timeout")); |
| 265 | } |
| 266 | |
| 267 | void |
| 268 | Nlsrc::onControlResponse(const std::string& info, const ndn::Data& data) |
| 269 | { |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 270 | if (data.getMetaInfo().getType() == ndn::tlv::ContentType_Nack) { |
| 271 | std::cerr << "ERROR: Run-time advertise/withdraw disabled" << std::endl; |
| 272 | return; |
| 273 | } |
| 274 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 275 | ndn::nfd::ControlResponse response; |
| 276 | |
| 277 | try { |
| 278 | response.wireDecode(data.getContent().blockFromValue()); |
| 279 | } |
| 280 | catch (const std::exception& e) { |
| 281 | std::cerr << "ERROR: Control response decoding error" << std::endl; |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | uint32_t code = response.getCode(); |
| 286 | |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 287 | if (code != RESPONSE_CODE_SUCCESS && code != RESPONSE_CODE_SAVE_OR_DELETE) { |
| 288 | |
| 289 | std::cerr << response.getText() << std::endl; |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 290 | std::cerr << "Name prefix update error (code: " << code << ")" << std::endl; |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | std::cout << "Applied Name prefix update successfully: " << info << std::endl; |
| 295 | } |
| 296 | |
| 297 | void |
| 298 | Nlsrc::fetchAdjacencyLsas() |
| 299 | { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 300 | fetchFromLsdb<nlsr::AdjLsa>(nlsr::dataset::ADJACENCY_COMPONENT, |
| 301 | std::bind(&Nlsrc::recordLsa, this, _1)); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void |
| 305 | Nlsrc::fetchCoordinateLsas() |
| 306 | { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 307 | fetchFromLsdb<nlsr::CoordinateLsa>(nlsr::dataset::COORDINATE_COMPONENT, |
| 308 | std::bind(&Nlsrc::recordLsa, this, _1)); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | void |
| 312 | Nlsrc::fetchNameLsas() |
| 313 | { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 314 | fetchFromLsdb<nlsr::NameLsa>(nlsr::dataset::NAME_COMPONENT, |
| 315 | std::bind(&Nlsrc::recordLsa, this, _1)); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 316 | } |
| 317 | |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 318 | void |
| 319 | Nlsrc::fetchRtables() |
| 320 | { |
Ashlesh Gawande | 0af4627 | 2020-12-12 16:45:13 -0500 | [diff] [blame] | 321 | fetchFromRt<nlsr::RoutingTableStatus>([this] (const auto& rts) { this->recordRtable(rts); }); |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 322 | } |
| 323 | |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 324 | template<class T> |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 325 | void |
| 326 | Nlsrc::fetchFromLsdb(const ndn::Name::Component& datasetType, |
| 327 | const std::function<void(const T&)>& recordLsa) |
| 328 | { |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 329 | auto name = m_routerPrefix; |
| 330 | name.append(LSDB_SUFFIX); |
| 331 | name.append(datasetType); |
| 332 | ndn::Interest interest(name); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 333 | |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 334 | auto fetcher = ndn::util::SegmentFetcher::start(m_face, interest, *m_validator); |
Ashlesh Gawande | 05cb728 | 2018-08-30 14:39:41 -0500 | [diff] [blame] | 335 | fetcher->onComplete.connect(std::bind(&Nlsrc::onFetchSuccess<T>, this, _1, recordLsa)); |
| 336 | fetcher->onError.connect(std::bind(&Nlsrc::onTimeout, this, _1, _2)); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 337 | } |
| 338 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 339 | void |
| 340 | Nlsrc::recordLsa(const nlsr::Lsa& lsa) |
| 341 | { |
| 342 | Router& router = m_routers.emplace(lsa.getOriginRouter(), Router()).first->second; |
| 343 | |
| 344 | if (lsa.getType() == nlsr::Lsa::Type::ADJACENCY) { |
| 345 | router.adjacencyLsaString = lsa.toString(); |
| 346 | } |
| 347 | else if (lsa.getType() == nlsr::Lsa::Type::COORDINATE) { |
| 348 | router.coordinateLsaString = lsa.toString(); |
| 349 | } |
| 350 | else if (lsa.getType() == nlsr::Lsa::Type::NAME) { |
| 351 | router.nameLsaString = lsa.toString(); |
| 352 | } |
| 353 | } |
| 354 | |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 355 | template<class T> |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 356 | void |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 357 | Nlsrc::fetchFromRt(const std::function<void(const T&)>& recordDataset) |
| 358 | { |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 359 | auto name = m_routerPrefix; |
| 360 | name.append(RT_SUFFIX); |
| 361 | ndn::Interest interest(name); |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 362 | |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 363 | auto fetcher = ndn::util::SegmentFetcher::start(m_face, interest, *m_validator); |
Ashlesh Gawande | 05cb728 | 2018-08-30 14:39:41 -0500 | [diff] [blame] | 364 | fetcher->onComplete.connect(std::bind(&Nlsrc::onFetchSuccess<T>, this, _1, recordDataset)); |
| 365 | fetcher->onError.connect(std::bind(&Nlsrc::onTimeout, this, _1, _2)); |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 366 | } |
| 367 | |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 368 | template<class T> |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 369 | void |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 370 | Nlsrc::onFetchSuccess(const ndn::ConstBufferPtr& buf, |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 371 | const std::function<void(const T&)>& recordDataset) |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 372 | { |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 373 | size_t offset = 0; |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 374 | while (offset < buf->size()) { |
| 375 | auto [isOk, block] = ndn::Block::fromBuffer(buf, offset); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 376 | |
| 377 | if (!isOk) { |
| 378 | std::cerr << "ERROR: cannot decode LSA TLV" << std::endl; |
| 379 | break; |
| 380 | } |
| 381 | |
| 382 | offset += block.size(); |
| 383 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 384 | T dataset(block); |
| 385 | recordDataset(dataset); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | runNextStep(); |
| 389 | } |
| 390 | |
| 391 | void |
| 392 | Nlsrc::onTimeout(uint32_t errorCode, const std::string& error) |
| 393 | { |
| 394 | std::cerr << "Request timed out (code: " << errorCode |
| 395 | << ", error: " << error << ")" << std::endl; |
| 396 | } |
| 397 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 398 | void |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 399 | Nlsrc::recordRtable(const nlsr::RoutingTableStatus& rts) |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 400 | { |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 401 | std::ostringstream os; |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 402 | os << rts; |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 403 | m_rtString = os.str(); |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | void |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 407 | Nlsrc::printLsdb() |
| 408 | { |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 409 | std::cout << "LSDB:" << std::endl; |
| 410 | |
| 411 | for (const auto& item : m_routers) { |
| 412 | std::cout << " OriginRouter: " << item.first << std::endl; |
| 413 | std::cout << std::endl; |
| 414 | |
| 415 | const Router& router = item.second; |
| 416 | |
| 417 | if (!router.adjacencyLsaString.empty()) { |
| 418 | std::cout << router.adjacencyLsaString << std::endl; |
| 419 | } |
| 420 | |
| 421 | if (!router.coordinateLsaString.empty()) { |
| 422 | std::cout << router.coordinateLsaString << std::endl; |
| 423 | } |
| 424 | |
| 425 | if (!router.nameLsaString.empty()) { |
| 426 | std::cout << router.nameLsaString << std::endl; |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 431 | void |
| 432 | Nlsrc::printRT() |
| 433 | { |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 434 | if (!m_rtString.empty()) { |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 435 | std::cout << m_rtString; |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 436 | } |
| 437 | else { |
| 438 | std::cout << "Routing Table is not calculated yet" << std::endl; |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
| 442 | void |
| 443 | Nlsrc::printAll() |
| 444 | { |
| 445 | std::cout << "NLSR Status" << std::endl; |
| 446 | printLsdb(); |
| 447 | printRT(); |
| 448 | } |
| 449 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 450 | } // namespace nlsrc |
| 451 | |
| 452 | //////////////////////////////////////////////////////////////////////////////// |
| 453 | //////////////////////////////////////////////////////////////////////////////// |
| 454 | |
| 455 | int |
| 456 | main(int argc, char** argv) |
| 457 | { |
| 458 | ndn::Face face; |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 459 | nlsrc::Nlsrc nlsrc(argv[0], face); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 460 | |
| 461 | if (argc < 2) { |
| 462 | nlsrc.printUsage(); |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | int opt; |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 467 | const char* confFile = DEFAULT_CONFIG_FILE; |
| 468 | bool disableValidator = false; |
| 469 | while ((opt = ::getopt(argc, argv, "hVR:c:k")) != -1) { |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 470 | switch (opt) { |
| 471 | case 'h': |
| 472 | nlsrc.printUsage(); |
| 473 | return 0; |
| 474 | case 'V': |
| 475 | std::cout << NLSR_VERSION_BUILD_STRING << std::endl; |
| 476 | return 0; |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 477 | case 'R': |
| 478 | nlsrc.setRouterPrefix(::optarg); |
| 479 | break; |
| 480 | case 'c': |
| 481 | confFile = ::optarg; |
| 482 | break; |
| 483 | case 'k': |
| 484 | disableValidator = true; |
| 485 | break; |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 486 | default: |
| 487 | nlsrc.printUsage(); |
| 488 | return 1; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | if (argc == ::optind) { |
| 493 | nlsrc.printUsage(); |
| 494 | return 1; |
| 495 | } |
| 496 | |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 497 | if (nlsrc.getRouterPrefix() != nlsrc::LOCALHOST_PREFIX && !disableValidator) { |
| 498 | if (!nlsrc.enableValidator(confFile)) { |
| 499 | return 1; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | std::vector<std::string> subcommand; |
| 504 | std::copy(&argv[::optind], &argv[argc], std::back_inserter(subcommand)); |
| 505 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 506 | try { |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 507 | bool isOk = nlsrc.dispatch(subcommand); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 508 | if (!isOk) { |
| 509 | nlsrc.printUsage(); |
| 510 | return 1; |
| 511 | } |
| 512 | |
| 513 | face.processEvents(); |
| 514 | } |
| 515 | catch (const std::exception& e) { |
| 516 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 517 | return 2; |
| 518 | } |
| 519 | return 0; |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 520 | } |