Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2016, 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. |
| 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/>. |
| 24 | */ |
| 25 | |
| 26 | #include "legacy-nfdc.hpp" |
| 27 | #include "face-id-fetcher.hpp" |
| 28 | |
Junxiao Shi | 2f74132 | 2016-08-29 00:53:18 +0000 | [diff] [blame] | 29 | #include <boost/program_options.hpp> |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 30 | #include <boost/regex.hpp> |
| 31 | |
| 32 | namespace nfd { |
| 33 | namespace tools { |
| 34 | namespace nfdc { |
| 35 | |
| 36 | using ndn::nfd::ControlParameters; |
| 37 | |
| 38 | const time::milliseconds LegacyNfdc::DEFAULT_EXPIRATION_PERIOD = time::milliseconds::max(); |
| 39 | const uint64_t LegacyNfdc::DEFAULT_COST = 0; |
| 40 | |
| 41 | LegacyNfdc::LegacyNfdc(ndn::Face& face) |
| 42 | : m_flags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT) |
| 43 | , m_cost(DEFAULT_COST) |
| 44 | , m_origin(ndn::nfd::ROUTE_ORIGIN_STATIC) |
| 45 | , m_expires(DEFAULT_EXPIRATION_PERIOD) |
| 46 | , m_facePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT) |
| 47 | , m_face(face) |
| 48 | , m_controller(face, m_keyChain) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | bool |
| 53 | LegacyNfdc::dispatch(const std::string& command) |
| 54 | { |
| 55 | if (command == "add-nexthop") { |
Junxiao Shi | 2f74132 | 2016-08-29 00:53:18 +0000 | [diff] [blame] | 56 | if (m_commandLineArguments.size() != 2) |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 57 | return false; |
| 58 | fibAddNextHop(); |
| 59 | } |
| 60 | else if (command == "remove-nexthop") { |
Junxiao Shi | 2f74132 | 2016-08-29 00:53:18 +0000 | [diff] [blame] | 61 | if (m_commandLineArguments.size() != 2) |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 62 | return false; |
| 63 | fibRemoveNextHop(); |
| 64 | } |
| 65 | else if (command == "register") { |
Junxiao Shi | 2f74132 | 2016-08-29 00:53:18 +0000 | [diff] [blame] | 66 | if (m_commandLineArguments.size() != 2) |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 67 | return false; |
| 68 | ribRegisterPrefix(); |
| 69 | } |
| 70 | else if (command == "unregister") { |
Junxiao Shi | 2f74132 | 2016-08-29 00:53:18 +0000 | [diff] [blame] | 71 | if (m_commandLineArguments.size() != 2) |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 72 | return false; |
| 73 | ribUnregisterPrefix(); |
| 74 | } |
| 75 | else if (command == "create") { |
Junxiao Shi | 2f74132 | 2016-08-29 00:53:18 +0000 | [diff] [blame] | 76 | if (m_commandLineArguments.size() != 1) |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 77 | return false; |
| 78 | faceCreate(); |
| 79 | } |
| 80 | else if (command == "destroy") { |
Junxiao Shi | 2f74132 | 2016-08-29 00:53:18 +0000 | [diff] [blame] | 81 | if (m_commandLineArguments.size() != 1) |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 82 | return false; |
| 83 | faceDestroy(); |
| 84 | } |
| 85 | else if (command == "set-strategy") { |
Junxiao Shi | 2f74132 | 2016-08-29 00:53:18 +0000 | [diff] [blame] | 86 | if (m_commandLineArguments.size() != 2) |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 87 | return false; |
| 88 | strategyChoiceSet(); |
| 89 | } |
| 90 | else if (command == "unset-strategy") { |
Junxiao Shi | 2f74132 | 2016-08-29 00:53:18 +0000 | [diff] [blame] | 91 | if (m_commandLineArguments.size() != 1) |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 92 | return false; |
| 93 | strategyChoiceUnset(); |
| 94 | } |
| 95 | else |
| 96 | return false; |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | LegacyNfdc::fibAddNextHop() |
| 103 | { |
| 104 | m_name = m_commandLineArguments[0]; |
| 105 | const std::string& faceName = m_commandLineArguments[1]; |
| 106 | |
| 107 | FaceIdFetcher::start(m_face, m_controller, faceName, true, |
| 108 | [this] (const uint32_t faceId) { |
| 109 | ControlParameters parameters; |
| 110 | parameters |
| 111 | .setName(m_name) |
| 112 | .setCost(m_cost) |
| 113 | .setFaceId(faceId); |
| 114 | |
| 115 | m_controller.start<ndn::nfd::FibAddNextHopCommand>(parameters, |
| 116 | bind(&LegacyNfdc::onSuccess, this, _1, "Nexthop insertion succeeded"), |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 117 | bind(&LegacyNfdc::onError, this, _1, "Nexthop insertion failed")); |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 118 | }, |
| 119 | bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1)); |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | LegacyNfdc::fibRemoveNextHop() |
| 124 | { |
| 125 | m_name = m_commandLineArguments[0]; |
| 126 | const std::string& faceName = m_commandLineArguments[1]; |
| 127 | |
| 128 | FaceIdFetcher::start(m_face, m_controller, faceName, false, |
| 129 | [this] (const uint32_t faceId) { |
| 130 | ControlParameters parameters; |
| 131 | parameters |
| 132 | .setName(m_name) |
| 133 | .setFaceId(faceId); |
| 134 | |
| 135 | m_controller.start<ndn::nfd::FibRemoveNextHopCommand>(parameters, |
| 136 | bind(&LegacyNfdc::onSuccess, this, _1, "Nexthop removal succeeded"), |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 137 | bind(&LegacyNfdc::onError, this, _1, "Nexthop removal failed")); |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 138 | }, |
| 139 | bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1)); |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | LegacyNfdc::ribRegisterPrefix() |
| 144 | { |
| 145 | m_name = m_commandLineArguments[0]; |
| 146 | const std::string& faceName = m_commandLineArguments[1]; |
| 147 | |
| 148 | FaceIdFetcher::start(m_face, m_controller, faceName, true, |
| 149 | [this] (const uint32_t faceId) { |
| 150 | ControlParameters parameters; |
| 151 | parameters |
| 152 | .setName(m_name) |
| 153 | .setCost(m_cost) |
| 154 | .setFlags(m_flags) |
| 155 | .setOrigin(m_origin) |
| 156 | .setFaceId(faceId); |
| 157 | |
| 158 | if (m_expires != DEFAULT_EXPIRATION_PERIOD) |
| 159 | parameters.setExpirationPeriod(m_expires); |
| 160 | |
| 161 | m_controller.start<ndn::nfd::RibRegisterCommand>(parameters, |
| 162 | bind(&LegacyNfdc::onSuccess, this, _1, "Successful in name registration"), |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 163 | bind(&LegacyNfdc::onError, this, _1, "Failed in name registration")); |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 164 | }, |
| 165 | bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1)); |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | LegacyNfdc::ribUnregisterPrefix() |
| 170 | { |
| 171 | m_name = m_commandLineArguments[0]; |
| 172 | const std::string& faceName = m_commandLineArguments[1]; |
| 173 | |
| 174 | FaceIdFetcher::start(m_face, m_controller, faceName, false, |
| 175 | [this] (const uint32_t faceId) { |
| 176 | ControlParameters parameters; |
| 177 | parameters |
| 178 | .setName(m_name) |
| 179 | .setFaceId(faceId) |
| 180 | .setOrigin(m_origin); |
| 181 | |
| 182 | m_controller.start<ndn::nfd::RibUnregisterCommand>(parameters, |
| 183 | bind(&LegacyNfdc::onSuccess, this, _1, "Successful in unregistering name"), |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 184 | bind(&LegacyNfdc::onError, this, _1, "Failed in unregistering name")); |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 185 | }, |
| 186 | bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1)); |
| 187 | } |
| 188 | |
| 189 | void |
| 190 | LegacyNfdc::onCanonizeFailure(const std::string& reason) |
| 191 | { |
| 192 | BOOST_THROW_EXCEPTION(Error(reason)); |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | LegacyNfdc::onObtainFaceIdFailure(const std::string& message) |
| 197 | { |
| 198 | BOOST_THROW_EXCEPTION(Error(message)); |
| 199 | } |
| 200 | |
| 201 | void |
| 202 | LegacyNfdc::faceCreate() |
| 203 | { |
| 204 | boost::regex e("^[a-z0-9]+\\:.*"); |
| 205 | if (!boost::regex_match(m_commandLineArguments[0], e)) |
| 206 | BOOST_THROW_EXCEPTION(Error("invalid uri format")); |
| 207 | |
| 208 | FaceUri faceUri; |
| 209 | faceUri.parse(m_commandLineArguments[0]); |
| 210 | |
| 211 | faceUri.canonize(bind(&LegacyNfdc::startFaceCreate, this, _1), |
| 212 | bind(&LegacyNfdc::onCanonizeFailure, this, _1), |
| 213 | m_face.getIoService(), time::seconds(4)); |
| 214 | } |
| 215 | |
| 216 | void |
| 217 | LegacyNfdc::startFaceCreate(const FaceUri& canonicalUri) |
| 218 | { |
| 219 | ControlParameters parameters; |
| 220 | parameters.setUri(canonicalUri.toString()); |
| 221 | parameters.setFacePersistency(m_facePersistency); |
| 222 | |
| 223 | m_controller.start<ndn::nfd::FaceCreateCommand>(parameters, |
| 224 | bind(&LegacyNfdc::onSuccess, this, _1, "Face creation succeeded"), |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 225 | bind(&LegacyNfdc::onError, this, _1, "Face creation failed")); |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void |
| 229 | LegacyNfdc::faceDestroy() |
| 230 | { |
| 231 | ControlParameters parameters; |
| 232 | const std::string& faceName = m_commandLineArguments[0]; |
| 233 | |
| 234 | FaceIdFetcher::start(m_face, m_controller, faceName, false, |
| 235 | [this] (const uint32_t faceId) { |
| 236 | ControlParameters faceParameters; |
| 237 | faceParameters.setFaceId(faceId); |
| 238 | |
| 239 | m_controller.start<ndn::nfd::FaceDestroyCommand>(faceParameters, |
| 240 | bind(&LegacyNfdc::onSuccess, this, _1, "Face destroy succeeded"), |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 241 | bind(&LegacyNfdc::onError, this, _1, "Face destroy failed")); |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 242 | }, |
| 243 | bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1)); |
| 244 | } |
| 245 | |
| 246 | void |
| 247 | LegacyNfdc::strategyChoiceSet() |
| 248 | { |
| 249 | const std::string& name = m_commandLineArguments[0]; |
| 250 | const std::string& strategy = m_commandLineArguments[1]; |
| 251 | |
| 252 | ControlParameters parameters; |
| 253 | parameters |
| 254 | .setName(name) |
| 255 | .setStrategy(strategy); |
| 256 | |
| 257 | m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters, |
| 258 | bind(&LegacyNfdc::onSuccess, this, _1, "Successfully set strategy choice"), |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 259 | bind(&LegacyNfdc::onError, this, _1, "Failed to set strategy choice")); |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void |
| 263 | LegacyNfdc::strategyChoiceUnset() |
| 264 | { |
| 265 | const std::string& name = m_commandLineArguments[0]; |
| 266 | |
| 267 | ControlParameters parameters; |
| 268 | parameters.setName(name); |
| 269 | |
| 270 | m_controller.start<ndn::nfd::StrategyChoiceUnsetCommand>(parameters, |
| 271 | bind(&LegacyNfdc::onSuccess, this, _1, "Successfully unset strategy choice"), |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 272 | bind(&LegacyNfdc::onError, this, _1, "Failed to unset strategy choice")); |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void |
| 276 | LegacyNfdc::onSuccess(const ControlParameters& commandSuccessResult, const std::string& message) |
| 277 | { |
| 278 | std::cout << message << ": " << commandSuccessResult << std::endl; |
| 279 | } |
| 280 | |
| 281 | void |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 282 | LegacyNfdc::onError(const ndn::nfd::ControlResponse& response, const std::string& message) |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 283 | { |
| 284 | std::ostringstream os; |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 285 | os << message << ": " << response.getText() << " (code: " << response.getCode() << ")"; |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 286 | BOOST_THROW_EXCEPTION(Error(os.str())); |
| 287 | } |
| 288 | |
Junxiao Shi | 2f74132 | 2016-08-29 00:53:18 +0000 | [diff] [blame] | 289 | void |
| 290 | legacyNfdcUsage() |
| 291 | { |
| 292 | std::cout << "Usage:\n" |
| 293 | "nfdc [-h] [-V] COMMAND [<Command Options>]\n" |
| 294 | " -h print usage and exit\n" |
| 295 | " -V print version and exit\n" |
| 296 | "\n" |
| 297 | " COMMAND can be one of the following:\n" |
| 298 | " register [-I] [-C] [-c cost] [-e expiration time] [-o origin] name <faceId | faceUri>\n" |
| 299 | " register name to the given faceId or faceUri\n" |
| 300 | " -I: unset CHILD_INHERIT flag\n" |
| 301 | " -C: set CAPTURE flag\n" |
| 302 | " -c: specify cost (default 0)\n" |
| 303 | " -e: specify expiration time in ms\n" |
| 304 | " (by default the entry remains in FIB for the lifetime of the associated face)\n" |
| 305 | " -o: specify origin\n" |
| 306 | " 0 for Local producer applications, 128 for NLSR, 255(default) for static routes\n" |
| 307 | " unregister [-o origin] name <faceId | faceUri>\n" |
| 308 | " unregister name from the given faceId\n" |
| 309 | " create [-P] <faceUri> \n" |
| 310 | " Create a face in one of the following formats:\n" |
| 311 | " UDP unicast: udp[4|6]://<remote-IP-or-host>[:<remote-port>]\n" |
| 312 | " TCP: tcp[4|6]://<remote-IP-or-host>[:<remote-port>] \n" |
| 313 | " -P: create permanent (instead of persistent) face\n" |
| 314 | " destroy <faceId | faceUri> \n" |
| 315 | " Destroy a face\n" |
| 316 | " set-strategy <name> <strategy> \n" |
| 317 | " Set the strategy for a namespace \n" |
| 318 | " unset-strategy <name> \n" |
| 319 | " Unset the strategy for a namespace \n" |
| 320 | " add-nexthop [-c <cost>] <name> <faceId | faceUri>\n" |
| 321 | " Add a nexthop to a FIB entry\n" |
| 322 | " -c: specify cost (default 0)\n" |
| 323 | " remove-nexthop <name> <faceId | faceUri> \n" |
| 324 | " Remove a nexthop from a FIB entry\n" |
| 325 | << std::endl; |
| 326 | } |
| 327 | |
| 328 | int |
| 329 | legacyNfdcMain(const std::string& subcommand, const std::vector<std::string>& args) |
| 330 | { |
| 331 | ndn::Face face; |
| 332 | LegacyNfdc p(face); |
| 333 | |
| 334 | bool wantUnsetChildInherit = false; |
| 335 | bool wantCapture = false; |
| 336 | bool wantPermanentFace = false; |
| 337 | int64_t expires = -1; |
| 338 | |
| 339 | namespace po = boost::program_options; |
| 340 | po::options_description options; |
| 341 | options.add_options() |
| 342 | (",I", po::bool_switch(&wantUnsetChildInherit)) |
| 343 | (",C", po::bool_switch(&wantCapture)) |
| 344 | (",c", po::value<uint64_t>(&p.m_cost)) |
| 345 | (",e", po::value<int64_t>(&expires)) |
| 346 | (",o", po::value<uint64_t>(&p.m_origin)) |
| 347 | (",P", po::bool_switch(&wantPermanentFace)); |
| 348 | po::variables_map vm; |
| 349 | std::vector<std::string> unparsed; |
| 350 | try { |
| 351 | po::parsed_options parsed = po::command_line_parser(args).options(options).allow_unregistered().run(); |
| 352 | unparsed = po::collect_unrecognized(parsed.options, po::include_positional); |
| 353 | po::store(parsed, vm); |
| 354 | po::notify(vm); |
| 355 | } |
| 356 | catch (const po::error& e) { |
| 357 | std::cerr << e.what() << std::endl; |
| 358 | legacyNfdcUsage(); |
| 359 | return 1; |
| 360 | } |
| 361 | |
| 362 | if (wantUnsetChildInherit) { |
| 363 | p.m_flags &= ~(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT); |
| 364 | } |
| 365 | if (wantCapture) { |
| 366 | p.m_flags |= ndn::nfd::ROUTE_FLAG_CAPTURE; |
| 367 | } |
| 368 | if (expires >= 0) { |
| 369 | // accept negative values as no expiration |
| 370 | p.m_expires = time::milliseconds(expires); |
| 371 | } |
| 372 | if (wantPermanentFace) { |
| 373 | p.m_facePersistency = ndn::nfd::FACE_PERSISTENCY_PERMANENT; |
| 374 | } |
| 375 | |
| 376 | if (std::any_of(unparsed.begin(), unparsed.end(), |
| 377 | [] (const std::string& s) { return s.empty() || s[0] == '-'; })) { |
| 378 | // unrecognized -option |
| 379 | legacyNfdcUsage(); |
| 380 | return 1; |
| 381 | } |
| 382 | p.m_commandLineArguments = unparsed; |
| 383 | |
| 384 | try { |
| 385 | bool isOk = p.dispatch(subcommand); |
| 386 | if (!isOk) { |
| 387 | legacyNfdcUsage(); |
| 388 | return 1; |
| 389 | } |
| 390 | face.processEvents(); |
| 391 | } |
| 392 | catch (const std::exception& e) { |
| 393 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 394 | return 2; |
| 395 | } |
| 396 | return 0; |
| 397 | } |
| 398 | |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 399 | } // namespace nfdc |
| 400 | } // namespace tools |
| 401 | } // namespace nfd |