blob: 433737586b6d07fc7c259d6df7646484440f434a [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/*
awlane879afac2025-04-28 17:59:36 -05003 * Copyright (c) 2014-2025, 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
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080022#include "lsa/adj-lsa.hpp"
23#include "lsa/coordinate-lsa.hpp"
24#include "lsa/name-lsa.hpp"
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070025#include "route/routing-table.hpp"
laqinfan35731852017-08-08 06:17:39 -050026
Vince Lehmanc439d662015-04-27 10:56:00 -050027#include <boost/noncopyable.hpp>
28#include <ndn-cxx/face.hpp>
29#include <ndn-cxx/security/key-chain.hpp>
Junxiao Shia3a63972022-01-24 02:03:41 +000030#include <ndn-cxx/security/validator.hpp>
Vince Lehmanc439d662015-04-27 10:56:00 -050031
32#include <deque>
33#include <map>
34#include <stdexcept>
35
36#ifndef NLSR_TOOLS_NLSRC_HPP
37#define NLSR_TOOLS_NLSRC_HPP
38
39namespace nlsrc {
40
41class Nlsrc : boost::noncopyable
42{
43public:
44 explicit
Junxiao Shia3a63972022-01-24 02:03:41 +000045 Nlsrc(std::string programName, ndn::Face& face);
Vince Lehmanc439d662015-04-27 10:56:00 -050046
47 void
Junxiao Shia3a63972022-01-24 02:03:41 +000048 printUsage() const;
49
50 const ndn::Name&
51 getRouterPrefix() const
52 {
53 return m_routerPrefix;
54 }
55
56 void
57 setRouterPrefix(ndn::Name prefix);
58
59 void
60 disableValidator();
61
62 bool
63 enableValidator(const std::string& filename);
Vince Lehmanc439d662015-04-27 10:56:00 -050064
65 void
laqinfan35731852017-08-08 06:17:39 -050066 getStatus(const std::string& command);
Vince Lehmanc439d662015-04-27 10:56:00 -050067
68 bool
Junxiao Shia3a63972022-01-24 02:03:41 +000069 dispatch(ndn::span<std::string> subcommand);
Vince Lehmanc439d662015-04-27 10:56:00 -050070
Junxiao Shif7ea1502023-06-23 04:30:19 +000071 int
72 getExitCode() const
73 {
74 return m_exitCode;
75 }
76
Vince Lehmanc439d662015-04-27 10:56:00 -050077private:
78 void
79 runNextStep();
80
81 /**
82 * \brief Adds a name prefix to be advertised in NLSR's Name LSA
83 *
84 * cmd format:
85 * name
86 *
87 */
88 void
awlane879afac2025-04-28 17:59:36 -050089 advertiseName(ndn::Name name, bool wantSave, uint64_t cost = 0);
Vince Lehmanc439d662015-04-27 10:56:00 -050090
91 /**
92 * \brief Removes a name prefix from NLSR's Name LSA
93 *
94 * cmd format:
95 * name
96 *
97 */
98 void
Junxiao Shia3a63972022-01-24 02:03:41 +000099 withdrawName(ndn::Name name, bool wantDelete);
Vince Lehmanc439d662015-04-27 10:56:00 -0500100
101 void
102 sendNamePrefixUpdate(const ndn::Name& name,
103 const ndn::Name::Component& verb,
Saurab Dulal7526cee2018-01-31 18:14:10 +0000104 const std::string& info,
awlane879afac2025-04-28 17:59:36 -0500105 bool saveFlag,
106 uint64_t cost = 0);
Vince Lehmanc439d662015-04-27 10:56:00 -0500107
108 void
109 onControlResponse(const std::string& info, const ndn::Data& data);
110
111private:
112 void
113 fetchAdjacencyLsas();
114
115 void
116 fetchCoordinateLsas();
117
118 void
119 fetchNameLsas();
120
Junxiao Shia3a63972022-01-24 02:03:41 +0000121 template<class T>
Vince Lehmanc439d662015-04-27 10:56:00 -0500122 void
123 fetchFromLsdb(const ndn::Name::Component& datasetType,
124 const std::function<void(const T&)>& recordLsa);
125
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700126 void
127 recordLsa(const nlsr::Lsa& lsa);
128
129 void
130 fetchRtables();
131
Junxiao Shia3a63972022-01-24 02:03:41 +0000132 template<class T>
Vince Lehmanc439d662015-04-27 10:56:00 -0500133 void
laqinfan35731852017-08-08 06:17:39 -0500134 fetchFromRt(const std::function<void(const T&)>& recordLsa);
135
Junxiao Shia3a63972022-01-24 02:03:41 +0000136 template<class T>
laqinfan35731852017-08-08 06:17:39 -0500137 void
Vince Lehmanc439d662015-04-27 10:56:00 -0500138 onFetchSuccess(const ndn::ConstBufferPtr& data,
139 const std::function<void(const T&)>& recordLsa);
140
141 void
142 onTimeout(uint32_t errorCode, const std::string& error);
143
Vince Lehmanc439d662015-04-27 10:56:00 -0500144 void
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700145 recordRtable(const nlsr::RoutingTableStatus& rts);
laqinfan35731852017-08-08 06:17:39 -0500146
147 void
Vince Lehmanc439d662015-04-27 10:56:00 -0500148 printLsdb();
149
laqinfan35731852017-08-08 06:17:39 -0500150 void
151 printRT();
152
153 void
154 printAll();
155
Vince Lehmanc439d662015-04-27 10:56:00 -0500156private:
Junxiao Shia3a63972022-01-24 02:03:41 +0000157 std::string m_programName;
158 ndn::Name m_routerPrefix;
159 std::unique_ptr<ndn::security::Validator> m_validator;
160 ndn::KeyChain m_keyChain;
161 ndn::Face& m_face;
162
Vince Lehmanc439d662015-04-27 10:56:00 -0500163 struct Router
164 {
165 std::string adjacencyLsaString;
166 std::string coordinateLsaString;
167 std::string nameLsaString;
168 };
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800169 std::map<ndn::Name, Router> m_routers;
laqinfana073e2e2018-01-15 21:17:24 +0000170 std::string m_rtString;
Vince Lehmanc439d662015-04-27 10:56:00 -0500171 std::deque<std::function<void()>> m_fetchSteps;
Junxiao Shif7ea1502023-06-23 04:30:19 +0000172
173 int m_exitCode = 0;
Vince Lehmanc439d662015-04-27 10:56:00 -0500174};
175
176} // namespace nlsrc
177
178#endif // NLSR_TOOLS_NLSRC_HPP