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