Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2018, 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/>. |
| 20 | **/ |
| 21 | |
| 22 | #include "tlv/adjacency-lsa.hpp" |
| 23 | #include "tlv/coordinate-lsa.hpp" |
| 24 | #include "tlv/name-lsa.hpp" |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame^] | 25 | #include "tlv/routing-table-status.hpp" |
| 26 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 27 | |
| 28 | #include <boost/noncopyable.hpp> |
| 29 | #include <ndn-cxx/face.hpp> |
| 30 | #include <ndn-cxx/security/key-chain.hpp> |
Muktadir R Chowdhury | 4c7caad | 2015-09-03 15:49:22 -0500 | [diff] [blame] | 31 | #include <ndn-cxx/security/validator-null.hpp> |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 32 | |
| 33 | #include <deque> |
| 34 | #include <map> |
| 35 | #include <stdexcept> |
| 36 | |
| 37 | #ifndef NLSR_TOOLS_NLSRC_HPP |
| 38 | #define NLSR_TOOLS_NLSRC_HPP |
| 39 | |
| 40 | namespace nlsrc { |
| 41 | |
| 42 | class Nlsrc : boost::noncopyable |
| 43 | { |
| 44 | public: |
| 45 | explicit |
| 46 | Nlsrc(ndn::Face& face); |
| 47 | |
| 48 | void |
| 49 | printUsage(); |
| 50 | |
| 51 | void |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame^] | 52 | getStatus(const std::string& command); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 53 | |
| 54 | bool |
| 55 | dispatch(const std::string& cmd); |
| 56 | |
| 57 | private: |
| 58 | void |
| 59 | runNextStep(); |
| 60 | |
| 61 | /** |
| 62 | * \brief Adds a name prefix to be advertised in NLSR's Name LSA |
| 63 | * |
| 64 | * cmd format: |
| 65 | * name |
| 66 | * |
| 67 | */ |
| 68 | void |
| 69 | advertiseName(); |
| 70 | |
| 71 | /** |
| 72 | * \brief Removes a name prefix from NLSR's Name LSA |
| 73 | * |
| 74 | * cmd format: |
| 75 | * name |
| 76 | * |
| 77 | */ |
| 78 | void |
| 79 | withdrawName(); |
| 80 | |
| 81 | void |
| 82 | sendNamePrefixUpdate(const ndn::Name& name, |
| 83 | const ndn::Name::Component& verb, |
| 84 | const std::string& info); |
| 85 | |
| 86 | void |
| 87 | onControlResponse(const std::string& info, const ndn::Data& data); |
| 88 | |
| 89 | private: |
| 90 | void |
| 91 | fetchAdjacencyLsas(); |
| 92 | |
| 93 | void |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame^] | 94 | fetchRtables(); |
| 95 | |
| 96 | void |
| 97 | fetchHRRtables(); |
| 98 | |
| 99 | void |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 100 | fetchCoordinateLsas(); |
| 101 | |
| 102 | void |
| 103 | fetchNameLsas(); |
| 104 | |
| 105 | template <class T> |
| 106 | void |
| 107 | fetchFromLsdb(const ndn::Name::Component& datasetType, |
| 108 | const std::function<void(const T&)>& recordLsa); |
| 109 | |
| 110 | template <class T> |
| 111 | void |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame^] | 112 | fetchFromRt(const std::function<void(const T&)>& recordLsa); |
| 113 | |
| 114 | template <class T> |
| 115 | void |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 116 | onFetchSuccess(const ndn::ConstBufferPtr& data, |
| 117 | const std::function<void(const T&)>& recordLsa); |
| 118 | |
| 119 | void |
| 120 | onTimeout(uint32_t errorCode, const std::string& error); |
| 121 | |
| 122 | private: |
| 123 | std::string |
| 124 | getLsaInfoString(const nlsr::tlv::LsaInfo& info); |
| 125 | |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame^] | 126 | std::string |
| 127 | getDesString(const nlsr::tlv::Destination& des); |
| 128 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 129 | void |
| 130 | recordAdjacencyLsa(const nlsr::tlv::AdjacencyLsa& lsa); |
| 131 | |
| 132 | void |
| 133 | recordCoordinateLsa(const nlsr::tlv::CoordinateLsa& lsa); |
| 134 | |
| 135 | void |
| 136 | recordNameLsa(const nlsr::tlv::NameLsa& lsa); |
| 137 | |
| 138 | void |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame^] | 139 | recordRtable(const nlsr::tlv::RoutingTable& rts); |
| 140 | |
| 141 | void |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 142 | printLsdb(); |
| 143 | |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame^] | 144 | void |
| 145 | printRT(); |
| 146 | |
| 147 | void |
| 148 | printAll(); |
| 149 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 150 | public: |
| 151 | const char* programName; |
| 152 | |
| 153 | // command parameters without leading 'cmd' component |
| 154 | const char* const* commandLineArguments; |
| 155 | int nOptions; |
| 156 | |
| 157 | private: |
| 158 | struct Router |
| 159 | { |
| 160 | std::string adjacencyLsaString; |
| 161 | std::string coordinateLsaString; |
| 162 | std::string nameLsaString; |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame^] | 163 | std::string rtString; |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | Router& |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame^] | 167 | getRouterLsdb(const nlsr::tlv::LsaInfo& info); |
| 168 | |
| 169 | Router& |
| 170 | getRouterRT(const nlsr::tlv::Destination& des); |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 171 | |
| 172 | typedef std::map<const ndn::Name, Router> RouterMap; |
| 173 | RouterMap m_routers; |
| 174 | |
| 175 | private: |
| 176 | ndn::KeyChain m_keyChain; |
| 177 | ndn::Face& m_face; |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 178 | ndn::security::ValidatorNull m_validator; |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame^] | 179 | std::string commandString; |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 180 | |
| 181 | std::deque<std::function<void()>> m_fetchSteps; |
| 182 | |
| 183 | static const ndn::Name LOCALHOST_PREFIX; |
| 184 | static const ndn::Name LSDB_PREFIX; |
| 185 | static const ndn::Name NAME_UPDATE_PREFIX; |
| 186 | |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame^] | 187 | static const ndn::Name RT_PREFIX; |
| 188 | |
Vince Lehman | c439d66 | 2015-04-27 10:56:00 -0500 | [diff] [blame] | 189 | static const uint32_t ERROR_CODE_TIMEOUT; |
| 190 | static const uint32_t RESPONSE_CODE_SUCCESS; |
| 191 | }; |
| 192 | |
| 193 | } // namespace nlsrc |
| 194 | |
| 195 | #endif // NLSR_TOOLS_NLSRC_HPP |