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