blob: adeaeee01ca2c5843d0c9a29f84e1db35dc93389 [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 Gawande0af46272020-12-12 16:45:13 -0500276 fetchFromRt<nlsr::RoutingTableStatus>([this] (const auto& rts) { this->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{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700284 ndn::Interest interest(ndn::Name(LSDB_PREFIX).append(datasetType));
Vince Lehmanc439d662015-04-27 10:56:00 -0500285
Ashlesh Gawande05cb7282018-08-30 14:39:41 -0500286 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 Lehmanc439d662015-04-27 10:56:00 -0500289}
290
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700291void
292Nlsrc::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 Lehmanc439d662015-04-27 10:56:00 -0500307template <class T>
308void
laqinfan35731852017-08-08 06:17:39 -0500309Nlsrc::fetchFromRt(const std::function<void(const T&)>& recordDataset)
310{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700311 ndn::Interest interest(RT_PREFIX);
laqinfan35731852017-08-08 06:17:39 -0500312
Ashlesh Gawande05cb7282018-08-30 14:39:41 -0500313 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));
laqinfan35731852017-08-08 06:17:39 -0500316}
317
318template <class T>
319void
Vince Lehmanc439d662015-04-27 10:56:00 -0500320Nlsrc::onFetchSuccess(const ndn::ConstBufferPtr& data,
laqinfan35731852017-08-08 06:17:39 -0500321 const std::function<void(const T&)>& recordDataset)
Vince Lehmanc439d662015-04-27 10:56:00 -0500322{
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
laqinfan35731852017-08-08 06:17:39 -0500337 T data(block);
338 recordDataset(data);
Vince Lehmanc439d662015-04-27 10:56:00 -0500339 }
340
341 runNextStep();
342}
343
344void
345Nlsrc::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 Lehmanc439d662015-04-27 10:56:00 -0500351void
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700352Nlsrc::recordRtable(const nlsr::RoutingTableStatus& rts)
laqinfan35731852017-08-08 06:17:39 -0500353{
laqinfan35731852017-08-08 06:17:39 -0500354 std::ostringstream os;
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700355 os << rts;
laqinfana073e2e2018-01-15 21:17:24 +0000356 m_rtString = os.str();
laqinfan35731852017-08-08 06:17:39 -0500357}
358
359void
Vince Lehmanc439d662015-04-27 10:56:00 -0500360Nlsrc::printLsdb()
361{
Vince Lehmanc439d662015-04-27 10:56:00 -0500362 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
laqinfan35731852017-08-08 06:17:39 -0500384void
385Nlsrc::printRT()
386{
laqinfana073e2e2018-01-15 21:17:24 +0000387 if (!m_rtString.empty()) {
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700388 std::cout << m_rtString;
laqinfana073e2e2018-01-15 21:17:24 +0000389 }
390 else {
391 std::cout << "Routing Table is not calculated yet" << std::endl;
laqinfan35731852017-08-08 06:17:39 -0500392 }
393}
394
395void
396Nlsrc::printAll()
397{
398 std::cout << "NLSR Status" << std::endl;
399 printLsdb();
400 printRT();
401}
402
Vince Lehmanc439d662015-04-27 10:56:00 -0500403} // namespace nlsrc
404
405////////////////////////////////////////////////////////////////////////////////
406////////////////////////////////////////////////////////////////////////////////
407
408int
409main(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 Dulal7526cee2018-01-31 18:14:10 +0000442 ::optind = 3; // Set ::optind to the command's index
Vince Lehmanc439d662015-04-27 10:56:00 -0500443
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 Afanasyevf9f39102015-12-01 17:43:40 -0800461}