hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 24 | */ |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 25 | |
| 26 | #ifndef NFD_TOOLS_NFDC_HPP |
| 27 | #define NFD_TOOLS_NFDC_HPP |
| 28 | |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/face.hpp> |
Junxiao Shi | 8e273ca | 2014-11-12 00:42:29 -0700 | [diff] [blame] | 30 | #include <ndn-cxx/security/key-chain.hpp> |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 31 | #include <ndn-cxx/util/time.hpp> |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 32 | #include <ndn-cxx/management/nfd-controller.hpp> |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 33 | #include <ndn-cxx/util/face-uri.hpp> |
Muktadir R Chowdhury | 6c4a67a | 2015-09-07 22:13:01 -0500 | [diff] [blame^] | 34 | #include <ndn-cxx/security/validator-null.hpp> |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 35 | #include <memory> |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 36 | |
| 37 | namespace nfdc { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 38 | |
| 39 | using namespace ndn::nfd; |
| 40 | |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 41 | class Nfdc : boost::noncopyable |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 42 | { |
| 43 | public: |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 44 | |
| 45 | static const ndn::time::milliseconds DEFAULT_EXPIRATION_PERIOD; |
| 46 | static const uint64_t DEFAULT_COST; |
| 47 | |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 48 | class Error : public std::runtime_error |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 49 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 50 | public: |
| 51 | explicit |
| 52 | Error(const std::string& what) |
| 53 | : std::runtime_error(what) |
| 54 | { |
| 55 | } |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 56 | }; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 57 | |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 58 | class FaceIdFetcher |
| 59 | { |
| 60 | public: |
| 61 | typedef std::function<void(uint32_t)> SuccessCallback; |
| 62 | typedef std::function<void(const std::string&)> FailureCallback; |
| 63 | |
| 64 | /** \brief obtain FaceId from input |
| 65 | * \param face Reference to the Face that should be used to fetch data |
| 66 | * \param controller Reference to the controller that should be used to sign the Interest |
| 67 | * \param input User input, either FaceId or FaceUri |
| 68 | * \param allowCreate Whether creating face is allowed |
| 69 | * \param onSucceed Callback to be fired when faceId is obtained |
| 70 | * \param onFail Callback to be fired when an error occurs |
| 71 | */ |
| 72 | static void |
| 73 | start(ndn::Face& face, |
| 74 | Controller& controller, |
| 75 | const std::string& input, |
| 76 | bool allowCreate, |
| 77 | const SuccessCallback& onSucceed, |
| 78 | const FailureCallback& onFail); |
| 79 | |
| 80 | private: |
| 81 | FaceIdFetcher(ndn::Face& face, |
| 82 | Controller& controller, |
| 83 | bool allowCreate, |
| 84 | const SuccessCallback& onSucceed, |
| 85 | const FailureCallback& onFail); |
| 86 | |
| 87 | void |
| 88 | onQuerySuccess(const ndn::ConstBufferPtr& data, |
| 89 | const ndn::util::FaceUri& canonicalUri); |
| 90 | |
| 91 | void |
| 92 | onQueryFailure(uint32_t errorCode, |
| 93 | const ndn::util::FaceUri& canonicalUri); |
| 94 | |
| 95 | void |
| 96 | onCanonizeSuccess(const ndn::util::FaceUri& canonicalUri); |
| 97 | |
| 98 | void |
| 99 | onCanonizeFailure(const std::string& reason); |
| 100 | |
| 101 | void |
| 102 | startGetFaceId(const ndn::util::FaceUri& faceUri); |
| 103 | |
| 104 | void |
| 105 | startFaceCreate(const ndn::util::FaceUri& canonicalUri); |
| 106 | |
| 107 | void |
| 108 | onFaceCreateError(uint32_t code, |
| 109 | const std::string& error, |
| 110 | const std::string& message); |
| 111 | |
| 112 | void |
| 113 | succeed(uint32_t faceId); |
| 114 | |
| 115 | void |
| 116 | fail(const std::string& reason); |
| 117 | |
| 118 | private: |
| 119 | ndn::Face& m_face; |
| 120 | Controller& m_controller; |
| 121 | bool m_allowCreate; |
| 122 | SuccessCallback m_onSucceed; |
| 123 | FailureCallback m_onFail; |
Muktadir R Chowdhury | 6c4a67a | 2015-09-07 22:13:01 -0500 | [diff] [blame^] | 124 | ndn::ValidatorNull m_validator; |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 127 | explicit |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 128 | Nfdc(ndn::Face& face); |
| 129 | |
| 130 | ~Nfdc(); |
| 131 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 132 | bool |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 133 | dispatch(const std::string& cmd); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 134 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 135 | /** |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 136 | * \brief Adds a nexthop to a FIB entry |
hilata | dc947ec | 2014-03-10 12:48:31 -0500 | [diff] [blame] | 137 | * |
| 138 | * If the FIB entry does not exist, it is inserted automatically |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 139 | * |
| 140 | * cmd format: |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 141 | * [-c cost] name faceId|faceUri |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 142 | * |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 143 | */ |
| 144 | void |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 145 | fibAddNextHop(); |
| 146 | |
| 147 | /** |
hilata | dc947ec | 2014-03-10 12:48:31 -0500 | [diff] [blame] | 148 | * \brief Removes a nexthop from an existing FIB entry |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 149 | * |
hilata | dc947ec | 2014-03-10 12:48:31 -0500 | [diff] [blame] | 150 | * If the last nexthop record in a FIB entry is removed, the FIB entry is also deleted |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 151 | * |
| 152 | * cmd format: |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 153 | * name faceId |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 154 | * |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 155 | */ |
| 156 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 157 | fibRemoveNextHop(); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 158 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 159 | /** |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 160 | * \brief Registers name to the given faceId or faceUri |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 161 | * |
| 162 | * cmd format: |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 163 | * [-I] [-C] [-c cost] name faceId|faceUri |
| 164 | */ |
| 165 | void |
| 166 | ribRegisterPrefix(); |
| 167 | |
| 168 | /** |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 169 | * \brief Unregisters name from the given faceId/faceUri |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 170 | * |
| 171 | * cmd format: |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 172 | * name faceId/faceUri |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 173 | */ |
| 174 | void |
| 175 | ribUnregisterPrefix(); |
| 176 | |
| 177 | /** |
| 178 | * \brief Creates new face |
| 179 | * |
| 180 | * This command allows creation of UDP unicast and TCP faces only |
| 181 | * |
| 182 | * cmd format: |
| 183 | * uri |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 184 | * |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 185 | */ |
| 186 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 187 | faceCreate(); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 188 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 189 | /** |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 190 | * \brief Destroys face |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 191 | * |
| 192 | * cmd format: |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 193 | * faceId|faceUri |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 194 | * |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 195 | */ |
| 196 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 197 | faceDestroy(); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 198 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 199 | /** |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 200 | * \brief Sets the strategy for a namespace |
| 201 | * |
| 202 | * cmd format: |
| 203 | * name strategy |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 204 | * |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 205 | */ |
| 206 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 207 | strategyChoiceSet(); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 208 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 209 | /** |
| 210 | * \brief Unset the strategy for a namespace |
| 211 | * |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 212 | * cmd format: |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 213 | * name strategy |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 214 | * |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 215 | */ |
| 216 | void |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 217 | strategyChoiceUnset(); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 218 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 219 | private: |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 220 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 221 | void |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 222 | onSuccess(const ControlParameters& commandSuccessResult, |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 223 | const std::string& message); |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 224 | |
| 225 | void |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 226 | onError(uint32_t code, const std::string& error, const std::string& message); |
| 227 | |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 228 | void |
| 229 | onCanonizeFailure(const std::string& reason); |
| 230 | |
| 231 | void |
| 232 | startFaceCreate(const ndn::util::FaceUri& canonicalUri); |
| 233 | |
| 234 | void |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 235 | onObtainFaceIdFailure(const std::string& message); |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 236 | |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 237 | public: |
| 238 | const char* m_programName; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 239 | |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 240 | // command parameters without leading 'cmd' component |
Junxiao Shi | 2229503 | 2014-04-29 22:57:40 -0700 | [diff] [blame] | 241 | const char* const* m_commandLineArguments; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 242 | int m_nOptions; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 243 | uint64_t m_flags; |
| 244 | uint64_t m_cost; |
| 245 | uint64_t m_faceId; |
Syed Obaid | 4ae0ce3 | 2014-06-17 13:59:20 -0500 | [diff] [blame] | 246 | uint64_t m_origin; |
| 247 | ndn::time::milliseconds m_expires; |
Obaid | dca5079 | 2014-04-24 18:38:40 -0500 | [diff] [blame] | 248 | std::string m_name; |
Joao Pereira | c6184cd | 2015-09-03 15:27:29 -0400 | [diff] [blame] | 249 | FacePersistency m_facePersistency; |
hilata | 54e4eaf | 2014-04-10 23:38:33 -0500 | [diff] [blame] | 250 | |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 251 | private: |
Junxiao Shi | 8e273ca | 2014-11-12 00:42:29 -0700 | [diff] [blame] | 252 | ndn::KeyChain m_keyChain; |
Chengyu Fan | 27be0b0 | 2014-11-24 20:52:53 -0700 | [diff] [blame] | 253 | ndn::Face& m_face; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 254 | Controller m_controller; |
Chengyu Fan | dae2530 | 2014-10-16 11:40:11 -0600 | [diff] [blame] | 255 | boost::asio::io_service& m_ioService; |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 256 | }; |
| 257 | |
hilata | 141eaae | 2014-03-13 19:54:47 -0500 | [diff] [blame] | 258 | } // namespace nfdc |
hilata | 198cadb | 2014-02-15 23:46:19 -0600 | [diff] [blame] | 259 | |
| 260 | #endif // NFD_TOOLS_NFDC_HPP |