blob: 1228bc8c6d5deec5478ca0ce9b389148c05e1e0f [file] [log] [blame]
Vince Lehmanc439d662015-04-27 10:56:00 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
laqinfan35731852017-08-08 06:17:39 -05003 * Copyright (c) 2014-2018, 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/>.
20 **/
21
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;
49
50Nlsrc::Nlsrc(ndn::Face& face)
51 : m_face(face)
52{
53}
54
55void
56Nlsrc::printUsage()
57{
58 std::cout << "Usage:\n" << programName << " [-h] [-V] COMMAND [<Command Options>]\n"
59 " -h print usage and exit\n"
60 " -V print version and exit\n"
61 "\n"
62 " COMMAND can be one of the following:\n"
laqinfan35731852017-08-08 06:17:39 -050063 " lsdb\n"
64 " display NLSR lsdb status\n"
65 " routing\n"
66 " display routing table status\n"
Vince Lehmanc439d662015-04-27 10:56:00 -050067 " status\n"
laqinfan35731852017-08-08 06:17:39 -050068 " display all NLSR status (lsdb & routingtable)\n"
Vince Lehmanc439d662015-04-27 10:56:00 -050069 " advertise name\n"
70 " advertise a name prefix through NLSR\n"
71 " withdraw name\n"
72 " remove a name prefix advertised through NLSR"
73 << std::endl;
74}
75
76void
laqinfan35731852017-08-08 06:17:39 -050077Nlsrc::getStatus(const std::string& command)
Vince Lehmanc439d662015-04-27 10:56:00 -050078{
laqinfan35731852017-08-08 06:17:39 -050079 if (command == "lsdb") {
80 m_fetchSteps.push_back(std::bind(&Nlsrc::fetchAdjacencyLsas, this));
81 m_fetchSteps.push_back(std::bind(&Nlsrc::fetchCoordinateLsas, this));
82 m_fetchSteps.push_back(std::bind(&Nlsrc::fetchNameLsas, this));
83 m_fetchSteps.push_back(std::bind(&Nlsrc::printLsdb, this));
84 }
85 else if (command == "routing") {
86 m_fetchSteps.push_back(std::bind(&Nlsrc::fetchRtables, this));
87 m_fetchSteps.push_back(std::bind(&Nlsrc::printRT, this));
88 }
89 else if(command == "status") {
90 m_fetchSteps.push_back(std::bind(&Nlsrc::fetchAdjacencyLsas, this));
91 m_fetchSteps.push_back(std::bind(&Nlsrc::fetchCoordinateLsas, this));
92 m_fetchSteps.push_back(std::bind(&Nlsrc::fetchNameLsas, this));
93 m_fetchSteps.push_back(std::bind(&Nlsrc::fetchRtables, this));
94 m_fetchSteps.push_back(std::bind(&Nlsrc::printAll, this));
95 }
Vince Lehmanc439d662015-04-27 10:56:00 -050096 runNextStep();
97}
98
99bool
100Nlsrc::dispatch(const std::string& command)
101{
102 if (command == "advertise") {
103 if (nOptions != 1) {
104 return false;
105 }
106
107 advertiseName();
108 return true;
109 }
110 else if (command == "withdraw") {
111 if (nOptions != 1) {
112 return false;
113 }
114
115 withdrawName();
116 return true;
117 }
laqinfan35731852017-08-08 06:17:39 -0500118 else if ((command == "lsdb")|| (command == "routing")||(command == "status")) {
Vince Lehmanc439d662015-04-27 10:56:00 -0500119 if (nOptions != 0) {
120 return false;
121 }
laqinfan35731852017-08-08 06:17:39 -0500122 commandString = command;
Vince Lehmanc439d662015-04-27 10:56:00 -0500123
laqinfan35731852017-08-08 06:17:39 -0500124 getStatus(command);
Vince Lehmanc439d662015-04-27 10:56:00 -0500125 return true;
126 }
127
128 return false;
129}
130
131void
132Nlsrc::runNextStep()
133{
134 if (m_fetchSteps.empty()) {
135 return;
136 }
137
138 std::function<void()> nextStep = m_fetchSteps.front();
139 m_fetchSteps.pop_front();
140
141 nextStep();
142}
143
144void
145Nlsrc::advertiseName()
146{
147 ndn::Name name = commandLineArguments[0];
148 ndn::Name::Component verb("advertise");
149 std::string info = "(Advertise: " + name.toUri() + ")";
150
151 sendNamePrefixUpdate(name, verb, info);
152}
153
154void
155Nlsrc::withdrawName()
156{
157 ndn::Name name = commandLineArguments[0];
158 ndn::Name::Component verb("withdraw");
159 std::string info = "(Withdraw: " + name.toUri() + ")";
160
161 sendNamePrefixUpdate(name, verb, info);
162}
163
164void
165Nlsrc::sendNamePrefixUpdate(const ndn::Name& name,
166 const ndn::Name::Component& verb,
167 const std::string& info)
168{
169 ndn::nfd::ControlParameters parameters;
170 parameters.setName(name);
171
172 ndn::Name commandName = NAME_UPDATE_PREFIX;
173 commandName.append(verb);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500174 commandName.append(parameters.wireEncode());
Vince Lehmanc439d662015-04-27 10:56:00 -0500175
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500176 ndn::security::CommandInterestSigner cis(m_keyChain);
Vince Lehmanc439d662015-04-27 10:56:00 -0500177
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500178 ndn::Interest commandInterest =
179 cis.makeCommandInterest(commandName,
180 ndn::security::signingByIdentity(m_keyChain.getPib().
181 getDefaultIdentity()));
182
183 commandInterest.setMustBeFresh(true);
184
185 m_face.expressInterest(commandInterest,
Vince Lehmanc439d662015-04-27 10:56:00 -0500186 std::bind(&Nlsrc::onControlResponse, this, info, _2),
Alexander Afanasyev1de901f2017-03-09 12:43:57 -0800187 std::bind(&Nlsrc::onTimeout, this, ERROR_CODE_TIMEOUT, "Nack"),
Vince Lehmanc439d662015-04-27 10:56:00 -0500188 std::bind(&Nlsrc::onTimeout, this, ERROR_CODE_TIMEOUT, "Timeout"));
189}
190
191void
192Nlsrc::onControlResponse(const std::string& info, const ndn::Data& data)
193{
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500194 if (data.getMetaInfo().getType() == ndn::tlv::ContentType_Nack) {
195 std::cerr << "ERROR: Run-time advertise/withdraw disabled" << std::endl;
196 return;
197 }
198
Vince Lehmanc439d662015-04-27 10:56:00 -0500199 ndn::nfd::ControlResponse response;
200
201 try {
202 response.wireDecode(data.getContent().blockFromValue());
203 }
204 catch (const std::exception& e) {
205 std::cerr << "ERROR: Control response decoding error" << std::endl;
206 return;
207 }
208
209 uint32_t code = response.getCode();
210
211 if (code != RESPONSE_CODE_SUCCESS) {
212 std::cerr << "Name prefix update error (code: " << code << ")" << std::endl;
213 return;
214 }
215
216 std::cout << "Applied Name prefix update successfully: " << info << std::endl;
217}
218
219void
220Nlsrc::fetchAdjacencyLsas()
221{
Nick Gordon114537f2017-08-09 14:51:37 -0500222 fetchFromLsdb<nlsr::tlv::AdjacencyLsa>(nlsr::dataset::ADJACENCY_COMPONENT,
Vince Lehmanc439d662015-04-27 10:56:00 -0500223 std::bind(&Nlsrc::recordAdjacencyLsa, this, _1));
224}
225
226void
227Nlsrc::fetchCoordinateLsas()
228{
Nick Gordon114537f2017-08-09 14:51:37 -0500229 fetchFromLsdb<nlsr::tlv::CoordinateLsa>(nlsr::dataset::COORDINATE_COMPONENT,
Vince Lehmanc439d662015-04-27 10:56:00 -0500230 std::bind(&Nlsrc::recordCoordinateLsa, this, _1));
231}
232
233void
234Nlsrc::fetchNameLsas()
235{
Nick Gordon114537f2017-08-09 14:51:37 -0500236 fetchFromLsdb<nlsr::tlv::NameLsa>(nlsr::dataset::NAME_COMPONENT,
Vince Lehmanc439d662015-04-27 10:56:00 -0500237 std::bind(&Nlsrc::recordNameLsa, this, _1));
238}
239
laqinfan35731852017-08-08 06:17:39 -0500240void
241Nlsrc::fetchRtables()
242{
laqinfana073e2e2018-01-15 21:17:24 +0000243 fetchFromRt<nlsr::tlv::RoutingTableStatus>(
244 [this] (const nlsr::tlv::RoutingTableStatus& rts) {
laqinfan35731852017-08-08 06:17:39 -0500245 recordRtable(rts);
246 });
247}
248
Vince Lehmanc439d662015-04-27 10:56:00 -0500249template <class T>
250void
251Nlsrc::fetchFromLsdb(const ndn::Name::Component& datasetType,
252 const std::function<void(const T&)>& recordLsa)
253{
254 ndn::Name command = LSDB_PREFIX;
255 command.append(datasetType);
256
257 ndn::Interest interest(command);
258
Ashlesh Gawande05cb7282018-08-30 14:39:41 -0500259 auto fetcher = ndn::util::SegmentFetcher::start(m_face, interest, m_validator);
260 fetcher->onComplete.connect(std::bind(&Nlsrc::onFetchSuccess<T>, this, _1, recordLsa));
261 fetcher->onError.connect(std::bind(&Nlsrc::onTimeout, this, _1, _2));
Vince Lehmanc439d662015-04-27 10:56:00 -0500262}
263
264template <class T>
265void
laqinfan35731852017-08-08 06:17:39 -0500266Nlsrc::fetchFromRt(const std::function<void(const T&)>& recordDataset)
267{
268 ndn::Name command = RT_PREFIX;
269
270 ndn::Interest interest(command);
271
Ashlesh Gawande05cb7282018-08-30 14:39:41 -0500272 auto fetcher = ndn::util::SegmentFetcher::start(m_face, interest, m_validator);
273 fetcher->onComplete.connect(std::bind(&Nlsrc::onFetchSuccess<T>, this, _1, recordDataset));
274 fetcher->onError.connect(std::bind(&Nlsrc::onTimeout, this, _1, _2));
laqinfan35731852017-08-08 06:17:39 -0500275}
276
277template <class T>
278void
Vince Lehmanc439d662015-04-27 10:56:00 -0500279Nlsrc::onFetchSuccess(const ndn::ConstBufferPtr& data,
laqinfan35731852017-08-08 06:17:39 -0500280 const std::function<void(const T&)>& recordDataset)
Vince Lehmanc439d662015-04-27 10:56:00 -0500281{
282 ndn::Block block;
283 size_t offset = 0;
284
285 while (offset < data->size()) {
286 bool isOk = false;
287 std::tie(isOk, block) = ndn::Block::fromBuffer(data, offset);
288
289 if (!isOk) {
290 std::cerr << "ERROR: cannot decode LSA TLV" << std::endl;
291 break;
292 }
293
294 offset += block.size();
295
laqinfan35731852017-08-08 06:17:39 -0500296 T data(block);
297 recordDataset(data);
Vince Lehmanc439d662015-04-27 10:56:00 -0500298 }
299
300 runNextStep();
301}
302
303void
304Nlsrc::onTimeout(uint32_t errorCode, const std::string& error)
305{
306 std::cerr << "Request timed out (code: " << errorCode
307 << ", error: " << error << ")" << std::endl;
308}
309
310std::string
311Nlsrc::getLsaInfoString(const nlsr::tlv::LsaInfo& info)
312{
313 std::ostringstream os;
314 os << " info=" << info;
315
316 return os.str();
317}
318
319void
320Nlsrc::recordAdjacencyLsa(const nlsr::tlv::AdjacencyLsa& lsa)
321{
laqinfan35731852017-08-08 06:17:39 -0500322 Router& router = getRouterLsdb(lsa.getLsaInfo());
Vince Lehmanc439d662015-04-27 10:56:00 -0500323
324 std::ostringstream os;
325 os << " AdjacencyLsa:" << std::endl;
326
327 os << getLsaInfoString(lsa.getLsaInfo()) << std::endl;
328
329 for (const auto& adjacency : lsa.getAdjacencies()) {
330 os << " adjacency=" << adjacency << std::endl;
331 }
332
333 router.adjacencyLsaString = os.str();
334}
335
336void
337Nlsrc::recordCoordinateLsa(const nlsr::tlv::CoordinateLsa& lsa)
338{
laqinfan35731852017-08-08 06:17:39 -0500339 Router& router = getRouterLsdb(lsa.getLsaInfo());
Vince Lehmanc439d662015-04-27 10:56:00 -0500340
341 std::ostringstream os;
342 os << " Coordinate LSA:" << std::endl;
343
344 os << getLsaInfoString(lsa.getLsaInfo()) << std::endl;
345
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600346 int i = 0;
347 for (auto const& value: lsa.getHyperbolicAngle()) {
348 os << " Hyp Angle " << i++ << ": "<< value << " ";
349 }
350 os << "\n radius=" << lsa.getHyperbolicRadius() << std::endl;
Vince Lehmanc439d662015-04-27 10:56:00 -0500351
352 router.coordinateLsaString = os.str();
353}
354
355void
356Nlsrc::recordNameLsa(const nlsr::tlv::NameLsa& lsa)
357{
laqinfan35731852017-08-08 06:17:39 -0500358 Router& router = getRouterLsdb(lsa.getLsaInfo());
Vince Lehmanc439d662015-04-27 10:56:00 -0500359
360 std::ostringstream os;
361 os << " Name LSA:" << std::endl;
362
363 os << getLsaInfoString(lsa.getLsaInfo()) << std::endl;
364
365 for (const auto& name : lsa.getNames()) {
366 os << " name=" << name << std::endl;
367 }
368
369 router.nameLsaString = os.str();
370}
371
372void
laqinfana073e2e2018-01-15 21:17:24 +0000373Nlsrc::recordRtable(const nlsr::tlv::RoutingTableStatus& rts)
laqinfan35731852017-08-08 06:17:39 -0500374{
laqinfan35731852017-08-08 06:17:39 -0500375 std::ostringstream os;
laqinfana073e2e2018-01-15 21:17:24 +0000376 for (const auto& rt : rts.getRoutingtable()) {
377 os << rt << std::endl;
laqinfan35731852017-08-08 06:17:39 -0500378 }
laqinfana073e2e2018-01-15 21:17:24 +0000379 m_rtString = os.str();
laqinfan35731852017-08-08 06:17:39 -0500380}
381
382void
Vince Lehmanc439d662015-04-27 10:56:00 -0500383Nlsrc::printLsdb()
384{
Vince Lehmanc439d662015-04-27 10:56:00 -0500385 std::cout << "LSDB:" << std::endl;
386
387 for (const auto& item : m_routers) {
388 std::cout << " OriginRouter: " << item.first << std::endl;
389 std::cout << std::endl;
390
391 const Router& router = item.second;
392
393 if (!router.adjacencyLsaString.empty()) {
394 std::cout << router.adjacencyLsaString << std::endl;
395 }
396
397 if (!router.coordinateLsaString.empty()) {
398 std::cout << router.coordinateLsaString << std::endl;
399 }
400
401 if (!router.nameLsaString.empty()) {
402 std::cout << router.nameLsaString << std::endl;
403 }
404 }
405}
406
laqinfan35731852017-08-08 06:17:39 -0500407void
408Nlsrc::printRT()
409{
laqinfana073e2e2018-01-15 21:17:24 +0000410 if (!m_rtString.empty()) {
411 std::cout << "Routing Table" << std::endl;
412 std::cout << m_rtString << std::endl;
413 }
414 else {
415 std::cout << "Routing Table is not calculated yet" << std::endl;
laqinfan35731852017-08-08 06:17:39 -0500416 }
417}
418
419void
420Nlsrc::printAll()
421{
422 std::cout << "NLSR Status" << std::endl;
423 printLsdb();
424 printRT();
425}
426
Vince Lehmanc439d662015-04-27 10:56:00 -0500427Nlsrc::Router&
laqinfan35731852017-08-08 06:17:39 -0500428Nlsrc::getRouterLsdb(const nlsr::tlv::LsaInfo& info)
Vince Lehmanc439d662015-04-27 10:56:00 -0500429{
430 const ndn::Name& originRouterName = info.getOriginRouter();
431
432 const auto& pair =
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800433 m_routers.insert(std::make_pair(originRouterName, Router()));
Vince Lehmanc439d662015-04-27 10:56:00 -0500434
435 return pair.first->second;
436}
437
438} // namespace nlsrc
439
440////////////////////////////////////////////////////////////////////////////////
441////////////////////////////////////////////////////////////////////////////////
442
443int
444main(int argc, char** argv)
445{
446 ndn::Face face;
447 nlsrc::Nlsrc nlsrc(face);
448
449 nlsrc.programName = argv[0];
450
451 if (argc < 2) {
452 nlsrc.printUsage();
453 return 0;
454 }
455
456 int opt;
457 while ((opt = ::getopt(argc, argv, "hV")) != -1) {
458 switch (opt) {
459 case 'h':
460 nlsrc.printUsage();
461 return 0;
462 case 'V':
463 std::cout << NLSR_VERSION_BUILD_STRING << std::endl;
464 return 0;
465 default:
466 nlsrc.printUsage();
467 return 1;
468 }
469 }
470
471 if (argc == ::optind) {
472 nlsrc.printUsage();
473 return 1;
474 }
475
476 try {
477 ::optind = 2; // Set ::optind to the command's index
478
479 nlsrc.commandLineArguments = argv + ::optind;
480 nlsrc.nOptions = argc - ::optind;
481
482 // argv[1] points to the command, so pass it to the dispatch
483 bool isOk = nlsrc.dispatch(argv[1]);
484 if (!isOk) {
485 nlsrc.printUsage();
486 return 1;
487 }
488
489 face.processEvents();
490 }
491 catch (const std::exception& e) {
492 std::cerr << "ERROR: " << e.what() << std::endl;
493 return 2;
494 }
495 return 0;
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800496}