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