blob: f5c4fa78b8aa369241d90c0810a0dfab47e38f7a [file] [log] [blame]
Junxiao Shid243d712016-08-19 06:45:31 +00001/* -*- 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 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
328int
Junxiao Shi737c43c2016-09-14 02:51:44 +0000329legacyNfdcMain(ExecuteContext& ctx)
Junxiao Shi2f741322016-08-29 00:53:18 +0000330{
Junxiao Shi737c43c2016-09-14 02:51:44 +0000331 LegacyNfdc p(ctx.face, ctx.keyChain);
Junxiao Shi2f741322016-08-29 00:53:18 +0000332
Junxiao Shi737c43c2016-09-14 02:51:44 +0000333 const std::string& subcommand = ctx.noun;
334 auto args = ctx.args.get<std::vector<std::string>>("args");
Junxiao Shi2f741322016-08-29 00:53:18 +0000335 bool wantUnsetChildInherit = false;
336 bool wantCapture = false;
337 bool wantPermanentFace = false;
338 int64_t expires = -1;
339
340 namespace po = boost::program_options;
341 po::options_description options;
342 options.add_options()
343 (",I", po::bool_switch(&wantUnsetChildInherit))
344 (",C", po::bool_switch(&wantCapture))
345 (",c", po::value<uint64_t>(&p.m_cost))
346 (",e", po::value<int64_t>(&expires))
347 (",o", po::value<uint64_t>(&p.m_origin))
348 (",P", po::bool_switch(&wantPermanentFace));
349 po::variables_map vm;
350 std::vector<std::string> unparsed;
351 try {
352 po::parsed_options parsed = po::command_line_parser(args).options(options).allow_unregistered().run();
353 unparsed = po::collect_unrecognized(parsed.options, po::include_positional);
354 po::store(parsed, vm);
355 po::notify(vm);
356 }
357 catch (const po::error& e) {
358 std::cerr << e.what() << std::endl;
359 legacyNfdcUsage();
360 return 1;
361 }
362
363 if (wantUnsetChildInherit) {
364 p.m_flags &= ~(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
365 }
366 if (wantCapture) {
367 p.m_flags |= ndn::nfd::ROUTE_FLAG_CAPTURE;
368 }
369 if (expires >= 0) {
370 // accept negative values as no expiration
371 p.m_expires = time::milliseconds(expires);
372 }
373 if (wantPermanentFace) {
374 p.m_facePersistency = ndn::nfd::FACE_PERSISTENCY_PERMANENT;
375 }
376
377 if (std::any_of(unparsed.begin(), unparsed.end(),
378 [] (const std::string& s) { return s.empty() || s[0] == '-'; })) {
379 // unrecognized -option
380 legacyNfdcUsage();
381 return 1;
382 }
383 p.m_commandLineArguments = unparsed;
384
Junxiao Shi737c43c2016-09-14 02:51:44 +0000385 bool isOk = p.dispatch(subcommand);
386 if (!isOk) {
387 legacyNfdcUsage();
388 return 1;
Junxiao Shi2f741322016-08-29 00:53:18 +0000389 }
Junxiao Shi737c43c2016-09-14 02:51:44 +0000390 ctx.face.processEvents();
Junxiao Shi2f741322016-08-29 00:53:18 +0000391 return 0;
392}
393
Junxiao Shid243d712016-08-19 06:45:31 +0000394} // namespace nfdc
395} // namespace tools
396} // namespace nfd