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 | /* |
Junxiao Shi | f7ea150 | 2023-06-23 04:30:19 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, 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 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 22 | #include "lsa/adj-lsa.hpp" |
| 23 | #include "lsa/coordinate-lsa.hpp" |
| 24 | #include "lsa/name-lsa.hpp" |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 25 | #include "route/routing-table.hpp" |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 26 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 27 | #include <boost/noncopyable.hpp> |
| 28 | #include <ndn-cxx/face.hpp> |
| 29 | #include <ndn-cxx/security/key-chain.hpp> |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 30 | #include <ndn-cxx/security/validator.hpp> |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 31 | |
| 32 | #include <deque> |
| 33 | #include <map> |
| 34 | #include <stdexcept> |
| 35 | |
| 36 | #ifndef NLSR_TOOLS_NLSRC_HPP |
| 37 | #define NLSR_TOOLS_NLSRC_HPP |
| 38 | |
| 39 | namespace nlsrc { |
| 40 | |
| 41 | class Nlsrc : boost::noncopyable |
| 42 | { |
| 43 | public: |
| 44 | explicit |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 45 | Nlsrc(std::string programName, ndn::Face& face); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 46 | |
| 47 | void |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 48 | 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 Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 64 | |
| 65 | void |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 66 | getStatus(const std::string& command); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 67 | |
| 68 | bool |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 69 | dispatch(ndn::span<std::string> subcommand); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 70 | |
Junxiao Shi | f7ea150 | 2023-06-23 04:30:19 +0000 | [diff] [blame] | 71 | int |
| 72 | getExitCode() const |
| 73 | { |
| 74 | return m_exitCode; |
| 75 | } |
| 76 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 77 | private: |
| 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 |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 89 | advertiseName(ndn::Name name, bool wantSave); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * \brief Removes a name prefix from NLSR's Name LSA |
| 93 | * |
| 94 | * cmd format: |
| 95 | * name |
| 96 | * |
| 97 | */ |
| 98 | void |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 99 | withdrawName(ndn::Name name, bool wantDelete); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 100 | |
| 101 | void |
| 102 | sendNamePrefixUpdate(const ndn::Name& name, |
| 103 | const ndn::Name::Component& verb, |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 104 | const std::string& info, |
| 105 | bool saveFlag); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 106 | |
| 107 | void |
| 108 | onControlResponse(const std::string& info, const ndn::Data& data); |
| 109 | |
| 110 | private: |
| 111 | void |
| 112 | fetchAdjacencyLsas(); |
| 113 | |
| 114 | void |
| 115 | fetchCoordinateLsas(); |
| 116 | |
| 117 | void |
| 118 | fetchNameLsas(); |
| 119 | |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 120 | template<class T> |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 121 | void |
| 122 | fetchFromLsdb(const ndn::Name::Component& datasetType, |
| 123 | const std::function<void(const T&)>& recordLsa); |
| 124 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 125 | void |
| 126 | recordLsa(const nlsr::Lsa& lsa); |
| 127 | |
| 128 | void |
| 129 | fetchRtables(); |
| 130 | |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 131 | template<class T> |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 132 | void |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 133 | fetchFromRt(const std::function<void(const T&)>& recordLsa); |
| 134 | |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 135 | template<class T> |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 136 | void |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 137 | onFetchSuccess(const ndn::ConstBufferPtr& data, |
| 138 | const std::function<void(const T&)>& recordLsa); |
| 139 | |
| 140 | void |
| 141 | onTimeout(uint32_t errorCode, const std::string& error); |
| 142 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 143 | void |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 144 | recordRtable(const nlsr::RoutingTableStatus& rts); |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 145 | |
| 146 | void |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 147 | printLsdb(); |
| 148 | |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 149 | void |
| 150 | printRT(); |
| 151 | |
| 152 | void |
| 153 | printAll(); |
| 154 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 155 | private: |
Junxiao Shi | a3a6397 | 2022-01-24 02:03:41 +0000 | [diff] [blame] | 156 | std::string m_programName; |
| 157 | ndn::Name m_routerPrefix; |
| 158 | std::unique_ptr<ndn::security::Validator> m_validator; |
| 159 | ndn::KeyChain m_keyChain; |
| 160 | ndn::Face& m_face; |
| 161 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 162 | struct Router |
| 163 | { |
| 164 | std::string adjacencyLsaString; |
| 165 | std::string coordinateLsaString; |
| 166 | std::string nameLsaString; |
| 167 | }; |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 168 | std::map<ndn::Name, Router> m_routers; |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 169 | std::string m_rtString; |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 170 | std::deque<std::function<void()>> m_fetchSteps; |
Junxiao Shi | f7ea150 | 2023-06-23 04:30:19 +0000 | [diff] [blame] | 171 | |
| 172 | int m_exitCode = 0; |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | } // namespace nlsrc |
| 176 | |
| 177 | #endif // NLSR_TOOLS_NLSRC_HPP |