blob: b54eb4327b96424a2f0e8398ffb29ae4e9879706 [file] [log] [blame]
Vince Lehmanc439d662015-04-27 10:56:00 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande0421bc62020-05-08 20:42:19 -07002/*
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08003 * Copyright (c) 2014-2020, The University of Memphis,
Vince Lehmanc439d662015-04-27 10:56:00 -05004 * 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 Gawande0421bc62020-05-08 20:42:19 -070020 */
Vince Lehmanc439d662015-04-27 10:56:00 -050021
22#include "nlsrc.hpp"
23
24#include "version.hpp"
laqinfan35731852017-08-08 06:17:39 -050025#include "src/publisher/dataset-interest-handler.hpp"
Vince Lehmanc439d662015-04-27 10:56:00 -050026
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 Shi3e5120c2016-09-10 16:58:34 +000031#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
32#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Vince Lehmanc439d662015-04-27 10:56:00 -050033#include <ndn-cxx/util/segment-fetcher.hpp>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050034#include <ndn-cxx/security/key-chain.hpp>
35#include <ndn-cxx/security/command-interest-signer.hpp>
Vince Lehmanc439d662015-04-27 10:56:00 -050036
37#include <iostream>
38
39namespace nlsrc {
40
41const ndn::Name Nlsrc::LOCALHOST_PREFIX = ndn::Name("/localhost/nlsr");
42const ndn::Name Nlsrc::LSDB_PREFIX = ndn::Name(Nlsrc::LOCALHOST_PREFIX).append("lsdb");
43const ndn::Name Nlsrc::NAME_UPDATE_PREFIX = ndn::Name(Nlsrc::LOCALHOST_PREFIX).append("prefix-update");
44
laqinfan35731852017-08-08 06:17:39 -050045const ndn::Name Nlsrc::RT_PREFIX = ndn::Name(Nlsrc::LOCALHOST_PREFIX).append("routing-table");
46
Vince Lehmanc439d662015-04-27 10:56:00 -050047const uint32_t Nlsrc::ERROR_CODE_TIMEOUT = 10060;
48const uint32_t Nlsrc::RESPONSE_CODE_SUCCESS = 200;
Saurab Dulal7526cee2018-01-31 18:14:10 +000049const uint32_t Nlsrc::RESPONSE_CODE_SAVE_OR_DELETE = 205;
Vince Lehmanc439d662015-04-27 10:56:00 -050050
51Nlsrc::Nlsrc(ndn::Face& face)
52 : m_face(face)
53{
54}
55
56void
57Nlsrc::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"
laqinfan35731852017-08-08 06:17:39 -050064 " lsdb\n"
65 " display NLSR lsdb status\n"
66 " routing\n"
67 " display routing table status\n"
Vince Lehmanc439d662015-04-27 10:56:00 -050068 " status\n"
laqinfan35731852017-08-08 06:17:39 -050069 " display all NLSR status (lsdb & routingtable)\n"
Vince Lehmanc439d662015-04-27 10:56:00 -050070 " advertise name\n"
71 " advertise a name prefix through NLSR\n"
Saurab Dulal7526cee2018-01-31 18:14:10 +000072 " advertise name save\n"
73 " advertise and save the name prefix to the conf file\n"
Vince Lehmanc439d662015-04-27 10:56:00 -050074 " withdraw name\n"
Saurab Dulal7526cee2018-01-31 18:14:10 +000075 " 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 Lehmanc439d662015-04-27 10:56:00 -050078 << std::endl;
79}
80
81void
laqinfan35731852017-08-08 06:17:39 -050082Nlsrc::getStatus(const std::string& command)
Vince Lehmanc439d662015-04-27 10:56:00 -050083{
laqinfan35731852017-08-08 06:17:39 -050084 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 Lehmanc439d662015-04-27 10:56:00 -0500101 runNextStep();
102}
103
104bool
105Nlsrc::dispatch(const std::string& command)
106{
107 if (command == "advertise") {
Saurab Dulal7526cee2018-01-31 18:14:10 +0000108 if (nOptions < 0) {
Vince Lehmanc439d662015-04-27 10:56:00 -0500109 return false;
110 }
Saurab Dulal7526cee2018-01-31 18:14:10 +0000111 else if (nOptions == 1) {
112 std::string saveFlag = commandLineArguments[0];
113 if (saveFlag != "save") {
114 return false;
115 }
116 }
Vince Lehmanc439d662015-04-27 10:56:00 -0500117
118 advertiseName();
119 return true;
120 }
121 else if (command == "withdraw") {
Saurab Dulal7526cee2018-01-31 18:14:10 +0000122 if (nOptions < 0) {
Vince Lehmanc439d662015-04-27 10:56:00 -0500123 return false;
124 }
Saurab Dulal7526cee2018-01-31 18:14:10 +0000125 else if (nOptions == 1) {
126 std::string saveFlag = commandLineArguments[0];
127 if (saveFlag != "delete") {
128 return false;
129 }
130 }
Vince Lehmanc439d662015-04-27 10:56:00 -0500131
132 withdrawName();
133 return true;
134 }
Saurab Dulal7526cee2018-01-31 18:14:10 +0000135 else if ((command == "lsdb") || (command == "routing") || (command == "status")) {
136 if (nOptions != -1) {
Vince Lehmanc439d662015-04-27 10:56:00 -0500137 return false;
138 }
laqinfan35731852017-08-08 06:17:39 -0500139 commandString = command;
Vince Lehmanc439d662015-04-27 10:56:00 -0500140
laqinfan35731852017-08-08 06:17:39 -0500141 getStatus(command);
Vince Lehmanc439d662015-04-27 10:56:00 -0500142 return true;
143 }
144
145 return false;
146}
147
148void
149Nlsrc::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
161void
162Nlsrc::advertiseName()
163{
Saurab Dulal7526cee2018-01-31 18:14:10 +0000164 ndn::Name name = commandLineArguments[-1];
Vince Lehmanc439d662015-04-27 10:56:00 -0500165
Saurab Dulal7526cee2018-01-31 18:14:10 +0000166 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 Lehmanc439d662015-04-27 10:56:00 -0500174}
175
176void
177Nlsrc::withdrawName()
178{
Saurab Dulal7526cee2018-01-31 18:14:10 +0000179 ndn::Name name = commandLineArguments[-1];
Vince Lehmanc439d662015-04-27 10:56:00 -0500180
Saurab Dulal7526cee2018-01-31 18:14:10 +0000181 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 Lehmanc439d662015-04-27 10:56:00 -0500189}
190
191void
192Nlsrc::sendNamePrefixUpdate(const ndn::Name& name,
193 const ndn::Name::Component& verb,
Saurab Dulal7526cee2018-01-31 18:14:10 +0000194 const std::string& info,
195 bool flag)
Vince Lehmanc439d662015-04-27 10:56:00 -0500196{
197 ndn::nfd::ControlParameters parameters;
198 parameters.setName(name);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000199 if (flag) {
200 parameters.setFlags(1);
201 }
Vince Lehmanc439d662015-04-27 10:56:00 -0500202
203 ndn::Name commandName = NAME_UPDATE_PREFIX;
204 commandName.append(verb);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500205 commandName.append(parameters.wireEncode());
Vince Lehmanc439d662015-04-27 10:56:00 -0500206
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500207 ndn::security::CommandInterestSigner cis(m_keyChain);
Vince Lehmanc439d662015-04-27 10:56:00 -0500208
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500209 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 Lehmanc439d662015-04-27 10:56:00 -0500217 std::bind(&Nlsrc::onControlResponse, this, info, _2),
Alexander Afanasyev1de901f2017-03-09 12:43:57 -0800218 std::bind(&Nlsrc::onTimeout, this, ERROR_CODE_TIMEOUT, "Nack"),
Vince Lehmanc439d662015-04-27 10:56:00 -0500219 std::bind(&Nlsrc::onTimeout, this, ERROR_CODE_TIMEOUT, "Timeout"));
220}
221
222void
223Nlsrc::onControlResponse(const std::string& info, const ndn::Data& data)
224{
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500225 if (data.getMetaInfo().getType() == ndn::tlv::ContentType_Nack) {
226 std::cerr << "ERROR: Run-time advertise/withdraw disabled" << std::endl;
227 return;
228 }
229
Vince Lehmanc439d662015-04-27 10:56:00 -0500230 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 Dulal7526cee2018-01-31 18:14:10 +0000242 if (code != RESPONSE_CODE_SUCCESS && code != RESPONSE_CODE_SAVE_OR_DELETE) {
243
244 std::cerr << response.getText() << std::endl;
Vince Lehmanc439d662015-04-27 10:56:00 -0500245 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
252void
253Nlsrc::fetchAdjacencyLsas()
254{
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800255 fetchFromLsdb<nlsr::AdjLsa>(nlsr::dataset::ADJACENCY_COMPONENT,
256 std::bind(&Nlsrc::recordLsa, this, _1));
Vince Lehmanc439d662015-04-27 10:56:00 -0500257}
258
259void
260Nlsrc::fetchCoordinateLsas()
261{
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800262 fetchFromLsdb<nlsr::CoordinateLsa>(nlsr::dataset::COORDINATE_COMPONENT,
263 std::bind(&Nlsrc::recordLsa, this, _1));
Vince Lehmanc439d662015-04-27 10:56:00 -0500264}
265
266void
267Nlsrc::fetchNameLsas()
268{
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800269 fetchFromLsdb<nlsr::NameLsa>(nlsr::dataset::NAME_COMPONENT,
270 std::bind(&Nlsrc::recordLsa, this, _1));
Vince Lehmanc439d662015-04-27 10:56:00 -0500271}
272
laqinfan35731852017-08-08 06:17:39 -0500273void
274Nlsrc::fetchRtables()
275{
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700276 fetchFromRt<nlsr::RoutingTableStatus>([this] (const auto& rts) { recordRtable(rts); });
laqinfan35731852017-08-08 06:17:39 -0500277}
278
Vince Lehmanc439d662015-04-27 10:56:00 -0500279template <class T>
280void
281Nlsrc::fetchFromLsdb(const ndn::Name::Component& datasetType,
282 const std::function<void(const T&)>& recordLsa)
283{
284 ndn::Name command = LSDB_PREFIX;
285 command.append(datasetType);
286
287 ndn::Interest interest(command);
288
Ashlesh Gawande05cb7282018-08-30 14:39:41 -0500289 auto fetcher = ndn::util::SegmentFetcher::start(m_face, interest, m_validator);
290 fetcher->onComplete.connect(std::bind(&Nlsrc::onFetchSuccess<T>, this, _1, recordLsa));
291 fetcher->onError.connect(std::bind(&Nlsrc::onTimeout, this, _1, _2));
Vince Lehmanc439d662015-04-27 10:56:00 -0500292}
293
294template <class T>
295void
laqinfan35731852017-08-08 06:17:39 -0500296Nlsrc::fetchFromRt(const std::function<void(const T&)>& recordDataset)
297{
298 ndn::Name command = RT_PREFIX;
299
300 ndn::Interest interest(command);
301
Ashlesh Gawande05cb7282018-08-30 14:39:41 -0500302 auto fetcher = ndn::util::SegmentFetcher::start(m_face, interest, m_validator);
303 fetcher->onComplete.connect(std::bind(&Nlsrc::onFetchSuccess<T>, this, _1, recordDataset));
304 fetcher->onError.connect(std::bind(&Nlsrc::onTimeout, this, _1, _2));
laqinfan35731852017-08-08 06:17:39 -0500305}
306
307template <class T>
308void
Vince Lehmanc439d662015-04-27 10:56:00 -0500309Nlsrc::onFetchSuccess(const ndn::ConstBufferPtr& data,
laqinfan35731852017-08-08 06:17:39 -0500310 const std::function<void(const T&)>& recordDataset)
Vince Lehmanc439d662015-04-27 10:56:00 -0500311{
312 ndn::Block block;
313 size_t offset = 0;
314
315 while (offset < data->size()) {
316 bool isOk = false;
317 std::tie(isOk, block) = ndn::Block::fromBuffer(data, offset);
318
319 if (!isOk) {
320 std::cerr << "ERROR: cannot decode LSA TLV" << std::endl;
321 break;
322 }
323
324 offset += block.size();
325
laqinfan35731852017-08-08 06:17:39 -0500326 T data(block);
327 recordDataset(data);
Vince Lehmanc439d662015-04-27 10:56:00 -0500328 }
329
330 runNextStep();
331}
332
333void
334Nlsrc::onTimeout(uint32_t errorCode, const std::string& error)
335{
336 std::cerr << "Request timed out (code: " << errorCode
337 << ", error: " << error << ")" << std::endl;
338}
339
Vince Lehmanc439d662015-04-27 10:56:00 -0500340void
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800341Nlsrc::recordLsa(const nlsr::Lsa& lsa)
Vince Lehmanc439d662015-04-27 10:56:00 -0500342{
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800343 const auto& pair = m_routers.emplace(lsa.getOriginRouter(), Router());
344 Router& router = pair.first->second;
Vince Lehmanc439d662015-04-27 10:56:00 -0500345
346 std::ostringstream os;
Vince Lehmanc439d662015-04-27 10:56:00 -0500347
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800348 switch (lsa.getType()) {
349 case nlsr::Lsa::Type::ADJACENCY: {
350 const nlsr::AdjLsa& adjLsa = static_cast<const nlsr::AdjLsa&>(lsa);
351 os << adjLsa;
352 router.adjacencyLsaString = os.str();
353 break;
354 }
355 case nlsr::Lsa::Type::COORDINATE: {
356 const nlsr::CoordinateLsa& coorLsa = static_cast<const nlsr::CoordinateLsa&>(lsa);
357 os << coorLsa;
358 router.coordinateLsaString = os.str();
359 break;
360 }
361 case nlsr::Lsa::Type::NAME: {
362 const nlsr::NameLsa& nameLsa = static_cast<const nlsr::NameLsa&>(lsa);
363 os << nameLsa;
364 router.nameLsaString = os.str();
365 break;
366 }
367 default:
368 break;
Vince Lehmanc439d662015-04-27 10:56:00 -0500369 }
Vince Lehmanc439d662015-04-27 10:56:00 -0500370}
371
372void
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700373Nlsrc::recordRtable(const nlsr::RoutingTableStatus& rts)
laqinfan35731852017-08-08 06:17:39 -0500374{
laqinfan35731852017-08-08 06:17:39 -0500375 std::ostringstream os;
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700376 os << rts;
laqinfana073e2e2018-01-15 21:17:24 +0000377 m_rtString = os.str();
laqinfan35731852017-08-08 06:17:39 -0500378}
379
380void
Vince Lehmanc439d662015-04-27 10:56:00 -0500381Nlsrc::printLsdb()
382{
Vince Lehmanc439d662015-04-27 10:56:00 -0500383 std::cout << "LSDB:" << std::endl;
384
385 for (const auto& item : m_routers) {
386 std::cout << " OriginRouter: " << item.first << std::endl;
387 std::cout << std::endl;
388
389 const Router& router = item.second;
390
391 if (!router.adjacencyLsaString.empty()) {
392 std::cout << router.adjacencyLsaString << std::endl;
393 }
394
395 if (!router.coordinateLsaString.empty()) {
396 std::cout << router.coordinateLsaString << std::endl;
397 }
398
399 if (!router.nameLsaString.empty()) {
400 std::cout << router.nameLsaString << std::endl;
401 }
402 }
403}
404
laqinfan35731852017-08-08 06:17:39 -0500405void
406Nlsrc::printRT()
407{
laqinfana073e2e2018-01-15 21:17:24 +0000408 if (!m_rtString.empty()) {
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700409 std::cout << m_rtString;
laqinfana073e2e2018-01-15 21:17:24 +0000410 }
411 else {
412 std::cout << "Routing Table is not calculated yet" << std::endl;
laqinfan35731852017-08-08 06:17:39 -0500413 }
414}
415
416void
417Nlsrc::printAll()
418{
419 std::cout << "NLSR Status" << std::endl;
420 printLsdb();
421 printRT();
422}
423
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800424std::string
425Nlsrc::getLsaInfo(const nlsr::Lsa& lsa)
Vince Lehmanc439d662015-04-27 10:56:00 -0500426{
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800427 auto duration = ndn::time::duration_cast<ndn::time::seconds>(lsa.getExpirationTimePoint() -
428 ndn::time::system_clock::now());
429 std::ostringstream os;
430 os << " LsaInfo("
431 << "OriginRouter: " << lsa.getOriginRouter() << ", "
432 << "SequenceNumber: " << lsa.getSeqNo() << ", "
433 << "ExpirationPeriod: " << duration;
434 os << ")\n\n";
Vince Lehmanc439d662015-04-27 10:56:00 -0500435
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800436 return os.str();
Vince Lehmanc439d662015-04-27 10:56:00 -0500437}
438
439} // namespace nlsrc
440
441////////////////////////////////////////////////////////////////////////////////
442////////////////////////////////////////////////////////////////////////////////
443
444int
445main(int argc, char** argv)
446{
447 ndn::Face face;
448 nlsrc::Nlsrc nlsrc(face);
449
450 nlsrc.programName = argv[0];
451
452 if (argc < 2) {
453 nlsrc.printUsage();
454 return 0;
455 }
456
457 int opt;
458 while ((opt = ::getopt(argc, argv, "hV")) != -1) {
459 switch (opt) {
460 case 'h':
461 nlsrc.printUsage();
462 return 0;
463 case 'V':
464 std::cout << NLSR_VERSION_BUILD_STRING << std::endl;
465 return 0;
466 default:
467 nlsrc.printUsage();
468 return 1;
469 }
470 }
471
472 if (argc == ::optind) {
473 nlsrc.printUsage();
474 return 1;
475 }
476
477 try {
Saurab Dulal7526cee2018-01-31 18:14:10 +0000478 ::optind = 3; // Set ::optind to the command's index
Vince Lehmanc439d662015-04-27 10:56:00 -0500479
480 nlsrc.commandLineArguments = argv + ::optind;
481 nlsrc.nOptions = argc - ::optind;
482
483 // argv[1] points to the command, so pass it to the dispatch
484 bool isOk = nlsrc.dispatch(argv[1]);
485 if (!isOk) {
486 nlsrc.printUsage();
487 return 1;
488 }
489
490 face.processEvents();
491 }
492 catch (const std::exception& e) {
493 std::cerr << "ERROR: " << e.what() << std::endl;
494 return 2;
495 }
496 return 0;
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800497}