blob: 5f4f81f9753081af0d6144ea8c93459b79bd4aee [file] [log] [blame]
Junxiao Shid243d712016-08-19 06:45:31 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi88a062d2017-01-26 03:10:05 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Junxiao Shid243d712016-08-19 06:45:31 +00004 * 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 Shi2f741322016-08-29 00:53:18 +000029#include <boost/program_options.hpp>
Junxiao Shid243d712016-08-19 06:45:31 +000030#include <boost/regex.hpp>
31
32namespace nfd {
33namespace tools {
34namespace nfdc {
35
36using ndn::nfd::ControlParameters;
37
38const time::milliseconds LegacyNfdc::DEFAULT_EXPIRATION_PERIOD = time::milliseconds::max();
39const uint64_t LegacyNfdc::DEFAULT_COST = 0;
40
Junxiao Shi737c43c2016-09-14 02:51:44 +000041LegacyNfdc::LegacyNfdc(Face& face, KeyChain& keyChain)
Junxiao Shid243d712016-08-19 06:45:31 +000042 : 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)
Junxiao Shi737c43c2016-09-14 02:51:44 +000048 , m_controller(face, keyChain)
Junxiao Shid243d712016-08-19 06:45:31 +000049{
50}
51
52bool
53LegacyNfdc::dispatch(const std::string& command)
54{
55 if (command == "add-nexthop") {
Junxiao Shi2f741322016-08-29 00:53:18 +000056 if (m_commandLineArguments.size() != 2)
Junxiao Shid243d712016-08-19 06:45:31 +000057 return false;
58 fibAddNextHop();
59 }
60 else if (command == "remove-nexthop") {
Junxiao Shi2f741322016-08-29 00:53:18 +000061 if (m_commandLineArguments.size() != 2)
Junxiao Shid243d712016-08-19 06:45:31 +000062 return false;
63 fibRemoveNextHop();
64 }
65 else if (command == "register") {
Junxiao Shi2f741322016-08-29 00:53:18 +000066 if (m_commandLineArguments.size() != 2)
Junxiao Shid243d712016-08-19 06:45:31 +000067 return false;
68 ribRegisterPrefix();
69 }
70 else if (command == "unregister") {
Junxiao Shi2f741322016-08-29 00:53:18 +000071 if (m_commandLineArguments.size() != 2)
Junxiao Shid243d712016-08-19 06:45:31 +000072 return false;
73 ribUnregisterPrefix();
74 }
75 else if (command == "create") {
Junxiao Shi2f741322016-08-29 00:53:18 +000076 if (m_commandLineArguments.size() != 1)
Junxiao Shid243d712016-08-19 06:45:31 +000077 return false;
78 faceCreate();
79 }
80 else if (command == "destroy") {
Junxiao Shi2f741322016-08-29 00:53:18 +000081 if (m_commandLineArguments.size() != 1)
Junxiao Shid243d712016-08-19 06:45:31 +000082 return false;
83 faceDestroy();
84 }
85 else if (command == "set-strategy") {
Junxiao Shi2f741322016-08-29 00:53:18 +000086 if (m_commandLineArguments.size() != 2)
Junxiao Shid243d712016-08-19 06:45:31 +000087 return false;
88 strategyChoiceSet();
89 }
90 else if (command == "unset-strategy") {
Junxiao Shi2f741322016-08-29 00:53:18 +000091 if (m_commandLineArguments.size() != 1)
Junxiao Shid243d712016-08-19 06:45:31 +000092 return false;
93 strategyChoiceUnset();
94 }
95 else
96 return false;
97
98 return true;
99}
100
101void
102LegacyNfdc::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 Shi29b41282016-08-22 03:47:02 +0000117 bind(&LegacyNfdc::onError, this, _1, "Nexthop insertion failed"));
Junxiao Shid243d712016-08-19 06:45:31 +0000118 },
119 bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1));
120}
121
122void
123LegacyNfdc::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 Shi29b41282016-08-22 03:47:02 +0000137 bind(&LegacyNfdc::onError, this, _1, "Nexthop removal failed"));
Junxiao Shid243d712016-08-19 06:45:31 +0000138 },
139 bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1));
140}
141
142void
143LegacyNfdc::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 Shi29b41282016-08-22 03:47:02 +0000163 bind(&LegacyNfdc::onError, this, _1, "Failed in name registration"));
Junxiao Shid243d712016-08-19 06:45:31 +0000164 },
165 bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1));
166}
167
168void
169LegacyNfdc::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 Shi29b41282016-08-22 03:47:02 +0000184 bind(&LegacyNfdc::onError, this, _1, "Failed in unregistering name"));
Junxiao Shid243d712016-08-19 06:45:31 +0000185 },
186 bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1));
187}
188
189void
190LegacyNfdc::onCanonizeFailure(const std::string& reason)
191{
192 BOOST_THROW_EXCEPTION(Error(reason));
193}
194
195void
196LegacyNfdc::onObtainFaceIdFailure(const std::string& message)
197{
198 BOOST_THROW_EXCEPTION(Error(message));
199}
200
201void
202LegacyNfdc::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
216void
217LegacyNfdc::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 Shi29b41282016-08-22 03:47:02 +0000225 bind(&LegacyNfdc::onError, this, _1, "Face creation failed"));
Junxiao Shid243d712016-08-19 06:45:31 +0000226}
227
228void
229LegacyNfdc::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 Shi29b41282016-08-22 03:47:02 +0000241 bind(&LegacyNfdc::onError, this, _1, "Face destroy failed"));
Junxiao Shid243d712016-08-19 06:45:31 +0000242 },
243 bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1));
244}
245
246void
247LegacyNfdc::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 Shi29b41282016-08-22 03:47:02 +0000259 bind(&LegacyNfdc::onError, this, _1, "Failed to set strategy choice"));
Junxiao Shid243d712016-08-19 06:45:31 +0000260}
261
262void
263LegacyNfdc::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 Shi29b41282016-08-22 03:47:02 +0000272 bind(&LegacyNfdc::onError, this, _1, "Failed to unset strategy choice"));
Junxiao Shid243d712016-08-19 06:45:31 +0000273}
274
275void
276LegacyNfdc::onSuccess(const ControlParameters& commandSuccessResult, const std::string& message)
277{
278 std::cout << message << ": " << commandSuccessResult << std::endl;
279}
280
281void
Junxiao Shi29b41282016-08-22 03:47:02 +0000282LegacyNfdc::onError(const ndn::nfd::ControlResponse& response, const std::string& message)
Junxiao Shid243d712016-08-19 06:45:31 +0000283{
284 std::ostringstream os;
Junxiao Shi29b41282016-08-22 03:47:02 +0000285 os << message << ": " << response.getText() << " (code: " << response.getCode() << ")";
Junxiao Shid243d712016-08-19 06:45:31 +0000286 BOOST_THROW_EXCEPTION(Error(os.str()));
287}
288
Junxiao Shi2f741322016-08-29 00:53:18 +0000289void
290legacyNfdcUsage()
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
Junxiao Shi88a062d2017-01-26 03:10:05 +0000328void
Junxiao Shid6958012017-02-20 03:34:48 +0000329legacyNfdcMain(ExecuteContext& ctx, const std::string& replacementCommand)
Junxiao Shi2f741322016-08-29 00:53:18 +0000330{
Junxiao Shid6958012017-02-20 03:34:48 +0000331 if (!replacementCommand.empty()) {
332 std::cerr << "'nfdc " << ctx.noun << "' command is deprecated. "
333 << "Use 'nfdc " << replacementCommand << "' instead.\n";
334 }
335
Junxiao Shi737c43c2016-09-14 02:51:44 +0000336 LegacyNfdc p(ctx.face, ctx.keyChain);
Junxiao Shi2f741322016-08-29 00:53:18 +0000337
Junxiao Shi737c43c2016-09-14 02:51:44 +0000338 const std::string& subcommand = ctx.noun;
339 auto args = ctx.args.get<std::vector<std::string>>("args");
Junxiao Shi2f741322016-08-29 00:53:18 +0000340 bool wantUnsetChildInherit = false;
341 bool wantCapture = false;
342 bool wantPermanentFace = false;
343 int64_t expires = -1;
344
345 namespace po = boost::program_options;
346 po::options_description options;
347 options.add_options()
348 (",I", po::bool_switch(&wantUnsetChildInherit))
349 (",C", po::bool_switch(&wantCapture))
350 (",c", po::value<uint64_t>(&p.m_cost))
351 (",e", po::value<int64_t>(&expires))
352 (",o", po::value<uint64_t>(&p.m_origin))
353 (",P", po::bool_switch(&wantPermanentFace));
354 po::variables_map vm;
355 std::vector<std::string> unparsed;
356 try {
357 po::parsed_options parsed = po::command_line_parser(args).options(options).allow_unregistered().run();
358 unparsed = po::collect_unrecognized(parsed.options, po::include_positional);
359 po::store(parsed, vm);
360 po::notify(vm);
361 }
362 catch (const po::error& e) {
363 std::cerr << e.what() << std::endl;
364 legacyNfdcUsage();
Junxiao Shi88a062d2017-01-26 03:10:05 +0000365 ctx.exitCode = 2;
366 return;
Junxiao Shi2f741322016-08-29 00:53:18 +0000367 }
368
369 if (wantUnsetChildInherit) {
370 p.m_flags &= ~(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
371 }
372 if (wantCapture) {
373 p.m_flags |= ndn::nfd::ROUTE_FLAG_CAPTURE;
374 }
375 if (expires >= 0) {
376 // accept negative values as no expiration
377 p.m_expires = time::milliseconds(expires);
378 }
379 if (wantPermanentFace) {
380 p.m_facePersistency = ndn::nfd::FACE_PERSISTENCY_PERMANENT;
381 }
382
383 if (std::any_of(unparsed.begin(), unparsed.end(),
384 [] (const std::string& s) { return s.empty() || s[0] == '-'; })) {
385 // unrecognized -option
386 legacyNfdcUsage();
Junxiao Shi88a062d2017-01-26 03:10:05 +0000387 ctx.exitCode = 2;
388 return;
Junxiao Shi2f741322016-08-29 00:53:18 +0000389 }
390 p.m_commandLineArguments = unparsed;
391
Junxiao Shi737c43c2016-09-14 02:51:44 +0000392 bool isOk = p.dispatch(subcommand);
393 if (!isOk) {
394 legacyNfdcUsage();
Junxiao Shi88a062d2017-01-26 03:10:05 +0000395 ctx.exitCode = 2;
396 return;
Junxiao Shi2f741322016-08-29 00:53:18 +0000397 }
Junxiao Shi737c43c2016-09-14 02:51:44 +0000398 ctx.face.processEvents();
Junxiao Shi2f741322016-08-29 00:53:18 +0000399}
400
Junxiao Shid243d712016-08-19 06:45:31 +0000401} // namespace nfdc
402} // namespace tools
403} // namespace nfd