blob: 7d9f763fa364656db4d1cd14c4b8262574574c68 [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 "core/version.hpp"
28
29#include <boost/lexical_cast.hpp>
30
31namespace nfd {
32namespace tools {
33namespace nfdc {
34
35static void
36usage(const char* programName)
37{
38 std::cout << "Usage:\n" << programName << " [-h] [-V] COMMAND [<Command Options>]\n"
39 " -h print usage and exit\n"
40 " -V print version and exit\n"
41 "\n"
42 " COMMAND can be one of the following:\n"
43 " register [-I] [-C] [-c cost] [-e expiration time] [-o origin] name <faceId | faceUri>\n"
44 " register name to the given faceId or faceUri\n"
45 " -I: unset CHILD_INHERIT flag\n"
46 " -C: set CAPTURE flag\n"
47 " -c: specify cost (default 0)\n"
48 " -e: specify expiration time in ms\n"
49 " (by default the entry remains in FIB for the lifetime of the associated face)\n"
50 " -o: specify origin\n"
51 " 0 for Local producer applications, 128 for NLSR, 255(default) for static routes\n"
52 " unregister [-o origin] name <faceId | faceUri>\n"
53 " unregister name from the given faceId\n"
54 " create [-P] <faceUri> \n"
55 " Create a face in one of the following formats:\n"
56 " UDP unicast: udp[4|6]://<remote-IP-or-host>[:<remote-port>]\n"
57 " TCP: tcp[4|6]://<remote-IP-or-host>[:<remote-port>] \n"
58 " -P: create permanent (instead of persistent) face\n"
59 " destroy <faceId | faceUri> \n"
60 " Destroy a face\n"
61 " set-strategy <name> <strategy> \n"
62 " Set the strategy for a namespace \n"
63 " unset-strategy <name> \n"
64 " Unset the strategy for a namespace \n"
65 " add-nexthop [-c <cost>] <name> <faceId | faceUri>\n"
66 " Add a nexthop to a FIB entry\n"
67 " -c: specify cost (default 0)\n"
68 " remove-nexthop <name> <faceId | faceUri> \n"
69 " Remove a nexthop from a FIB entry\n"
70 << std::endl;
71}
72
73static int
74main(int argc, char** argv)
75{
76 ndn::Face face;
77 LegacyNfdc p(face);
78
79 p.m_programName = argv[0];
80
81 if (argc < 2) {
82 usage(p.m_programName);
83 return 0;
84 }
85
86 if (!strcmp(argv[1], "-h")) {
87 usage(p.m_programName);
88 return 0;
89 }
90
91 if (!strcmp(argv[1], "-V")) {
92 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
93 return 0;
94 }
95
96 ::optind = 2; //start reading options from 2nd argument i.e. Command
97 int opt;
98 while ((opt = ::getopt(argc, argv, "ICc:e:o:P")) != -1) {
99 switch (opt) {
100 case 'I':
101 p.m_flags = p.m_flags & ~(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
102 break;
103
104 case 'C':
105 p.m_flags = p.m_flags | ndn::nfd::ROUTE_FLAG_CAPTURE;
106 break;
107
108 case 'c':
109 try {
110 p.m_cost = boost::lexical_cast<uint64_t>(::optarg);
111 }
112 catch (const boost::bad_lexical_cast&) {
113 std::cerr << "Error: cost must be in unsigned integer format" << std::endl;
114 return 1;
115 }
116 break;
117
118 case 'e':
119 uint64_t expires;
120 try {
121 expires = boost::lexical_cast<uint64_t>(::optarg);
122 }
123 catch (const boost::bad_lexical_cast&) {
124 std::cerr << "Error: expiration time must be in unsigned integer format" << std::endl;
125 return 1;
126 }
127 p.m_expires = ndn::time::milliseconds(expires);
128 break;
129
130 case 'o':
131 try {
132 p.m_origin = boost::lexical_cast<uint64_t>(::optarg);
133 }
134 catch (const boost::bad_lexical_cast&) {
135 std::cerr << "Error: origin must be in unsigned integer format" << std::endl;
136 return 1;
137 }
138 break;
139
140 case 'P':
141 p.m_facePersistency = ndn::nfd::FACE_PERSISTENCY_PERMANENT;
142 break;
143
144 default:
145 usage(p.m_programName);
146 return 1;
147 }
148 }
149
150 if (argc == ::optind) {
151 usage(p.m_programName);
152 return 1;
153 }
154
155 try {
156 p.m_commandLineArguments = argv + ::optind;
157 p.m_nOptions = argc - ::optind;
158
159 //argv[1] points to the command, so pass it to the dispatch
160 bool isOk = p.dispatch(argv[1]);
161 if (!isOk) {
162 usage(p.m_programName);
163 return 1;
164 }
165 face.processEvents();
166 }
167 catch (const std::exception& e) {
168 std::cerr << "ERROR: " << e.what() << std::endl;
169 return 2;
170 }
171 return 0;
172}
173
174} // namespace nfdc
175} // namespace tools
176} // namespace nfd
177
178int
179main(int argc, char** argv)
180{
181 return nfd::tools::nfdc::main(argc, argv);
182}