blob: ff9f2afe5f826d341b2afd4171f11f8edcf97af1 [file] [log] [blame]
Zhuo Lib3558892016-08-12 15:51:12 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shib54aabd2018-04-16 19:36:24 +00002/*
Davide Pesaventoa0e6b602021-01-21 19:47:04 -05003 * Copyright (c) 2014-2021, Regents of the University of California,
Zhuo Lib3558892016-08-12 15:51:12 -07004 * 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 ndn-tools (Named Data Networking Essential Tools).
12 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
13 *
14 * ndn-tools 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 * ndn-tools 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 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * @author Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
26 * @author Zhuo Li <zhuoli@email.arizona.edu>
Davide Pesavento96028232019-08-17 01:26:13 -040027 * @author Davide Pesavento <davidepesa@gmail.com>
Zhuo Lib3558892016-08-12 15:51:12 -070028 */
29
30#include "ndnpeek.hpp"
31#include "core/version.hpp"
32
33#include <ndn-cxx/util/io.hpp>
34
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050035#include <boost/program_options/options_description.hpp>
36#include <boost/program_options/parsers.hpp>
37#include <boost/program_options/variables_map.hpp>
38
Davide Pesavento50cf6db2019-07-29 20:40:10 -040039#include <cerrno>
40#include <cstring>
41#include <fstream>
42
Zhuo Lib3558892016-08-12 15:51:12 -070043namespace ndn {
44namespace peek {
45
46namespace po = boost::program_options;
47
48static void
Davide Pesavento65d11552019-06-09 19:15:50 -040049usage(std::ostream& os, const std::string& program, const po::options_description& options)
Zhuo Lib3558892016-08-12 15:51:12 -070050{
Davide Pesaventoe75861e2019-07-24 21:55:39 -040051 os << "Usage: " << program << " [options] /name\n"
Davide Pesavento65d11552019-06-09 19:15:50 -040052 << "\n"
53 << "Fetch one data item matching the specified name and write it to the standard output.\n"
Zhuo Lib3558892016-08-12 15:51:12 -070054 << options;
55}
56
Davide Pesavento50cf6db2019-07-29 20:40:10 -040057static std::ifstream
58openBinaryFile(const std::string& filename)
59{
60 std::ifstream file(filename, std::ios::binary);
61 if (!file) {
62 std::cerr << "ERROR: cannot open '" << filename << "' for reading: "
63 << std::strerror(errno) << std::endl;
64 }
65 return file;
66}
67
Zhuo Lib3558892016-08-12 15:51:12 -070068static int
69main(int argc, char* argv[])
70{
Davide Pesavento65d11552019-06-09 19:15:50 -040071 std::string progName(argv[0]);
Zhuo Lib3558892016-08-12 15:51:12 -070072 PeekOptions options;
Zhuo Lib3558892016-08-12 15:51:12 -070073
74 po::options_description genericOptDesc("Generic options");
75 genericOptDesc.add_options()
Davide Pesavento65d11552019-06-09 19:15:50 -040076 ("help,h", "print help and exit")
Davide Pesavento0da1f442019-07-26 17:38:13 -040077 ("payload,p", po::bool_switch(&options.wantPayloadOnly),
78 "print payload only, instead of full packet")
79 ("timeout,w", po::value<time::milliseconds::rep>(), "execution timeout, in milliseconds")
Davide Pesavento65d11552019-06-09 19:15:50 -040080 ("verbose,v", po::bool_switch(&options.isVerbose), "turn on verbose output")
Zhuo Lib3558892016-08-12 15:51:12 -070081 ("version,V", "print version and exit")
82 ;
83
84 po::options_description interestOptDesc("Interest construction");
85 interestOptDesc.add_options()
Davide Pesavento65d11552019-06-09 19:15:50 -040086 ("prefix,P", po::bool_switch(&options.canBePrefix), "set CanBePrefix")
87 ("fresh,f", po::bool_switch(&options.mustBeFresh), "set MustBeFresh")
Davide Pesavento50cf6db2019-07-29 20:40:10 -040088 ("link-file", po::value<std::string>(), "set ForwardingHint from a raw binary file")
Davide Pesavento0da1f442019-07-26 17:38:13 -040089 ("lifetime,l", po::value<time::milliseconds::rep>()->default_value(options.interestLifetime.count()),
90 "set InterestLifetime, in milliseconds")
Davide Pesaventoc214e072019-08-17 01:33:28 -040091 ("hop-limit,H", po::value<int>(), "set HopLimit")
Davide Pesavento96028232019-08-17 01:26:13 -040092 ("app-params,A", po::value<std::string>(), "set ApplicationParameters from a base64-encoded string")
93 ("app-params-file", po::value<std::string>(), "set ApplicationParameters from a file")
Zhuo Lib3558892016-08-12 15:51:12 -070094 ;
95
96 po::options_description visibleOptDesc;
97 visibleOptDesc.add(genericOptDesc).add(interestOptDesc);
98
99 po::options_description hiddenOptDesc;
100 hiddenOptDesc.add_options()
Junxiao Shib54aabd2018-04-16 19:36:24 +0000101 ("name", po::value<std::string>(), "Interest name");
Zhuo Lib3558892016-08-12 15:51:12 -0700102
103 po::options_description optDesc;
104 optDesc.add(visibleOptDesc).add(hiddenOptDesc);
105
106 po::positional_options_description optPos;
Junxiao Shib54aabd2018-04-16 19:36:24 +0000107 optPos.add("name", -1);
Zhuo Lib3558892016-08-12 15:51:12 -0700108
109 po::variables_map vm;
110 try {
111 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm);
112 po::notify(vm);
113 }
114 catch (const po::error& e) {
115 std::cerr << "ERROR: " << e.what() << std::endl;
116 return 2;
117 }
118
119 if (vm.count("help") > 0) {
Davide Pesavento65d11552019-06-09 19:15:50 -0400120 usage(std::cout, progName, visibleOptDesc);
Zhuo Lib3558892016-08-12 15:51:12 -0700121 return 0;
122 }
123
124 if (vm.count("version") > 0) {
125 std::cout << "ndnpeek " << tools::VERSION << std::endl;
126 return 0;
127 }
128
Davide Pesavento65d11552019-06-09 19:15:50 -0400129 if (vm.count("name") == 0) {
130 std::cerr << "ERROR: missing name\n\n";
131 usage(std::cerr, progName, visibleOptDesc);
132 return 2;
133 }
134
135 try {
Junxiao Shib54aabd2018-04-16 19:36:24 +0000136 options.name = vm["name"].as<std::string>();
Zhuo Lib3558892016-08-12 15:51:12 -0700137 }
Davide Pesavento65d11552019-06-09 19:15:50 -0400138 catch (const Name::Error& e) {
139 std::cerr << "ERROR: invalid name: " << e.what() << std::endl;
Zhuo Lib3558892016-08-12 15:51:12 -0700140 return 2;
141 }
142
Zhuo Lib3558892016-08-12 15:51:12 -0700143 if (vm.count("timeout") > 0) {
Davide Pesavento0da1f442019-07-26 17:38:13 -0400144 options.timeout = time::milliseconds(vm["timeout"].as<time::milliseconds::rep>());
145 if (*options.timeout < 0_ms) {
Davide Pesavento65d11552019-06-09 19:15:50 -0400146 std::cerr << "ERROR: timeout cannot be negative" << std::endl;
Zhuo Lib3558892016-08-12 15:51:12 -0700147 return 2;
148 }
149 }
150
151 if (vm.count("link-file") > 0) {
Davide Pesavento50cf6db2019-07-29 20:40:10 -0400152 auto filename = vm["link-file"].as<std::string>();
153 std::ifstream linkFile = openBinaryFile(filename);
154 if (!linkFile) {
Zhuo Lib3558892016-08-12 15:51:12 -0700155 return 2;
156 }
Davide Pesavento50cf6db2019-07-29 20:40:10 -0400157 options.link = io::load<Link>(linkFile, io::NO_ENCODING);
158 if (!options.link) {
159 std::cerr << "ERROR: cannot parse a valid Link object from file '" << filename << "'" << std::endl;
160 return 2;
161 }
162 }
163
164 options.interestLifetime = time::milliseconds(vm["lifetime"].as<time::milliseconds::rep>());
165 if (options.interestLifetime < 0_ms) {
Davide Pesaventoc214e072019-08-17 01:33:28 -0400166 std::cerr << "ERROR: InterestLifetime cannot be negative" << std::endl;
Davide Pesavento50cf6db2019-07-29 20:40:10 -0400167 return 2;
Zhuo Lib3558892016-08-12 15:51:12 -0700168 }
169
Davide Pesaventoc214e072019-08-17 01:33:28 -0400170 if (vm.count("hop-limit") > 0) {
171 auto hopLimit = vm["hop-limit"].as<int>();
172 if (hopLimit < 0 || hopLimit > 255) {
173 std::cerr << "ERROR: HopLimit must be between 0 and 255" << std::endl;
174 return 2;
175 }
176 options.hopLimit = static_cast<uint8_t>(hopLimit);
177 }
178
Davide Pesavento96028232019-08-17 01:26:13 -0400179 if (vm.count("app-params") > 0) {
180 if (vm.count("app-params-file") > 0) {
181 std::cerr << "ERROR: cannot specify both '--app-params' and '--app-params-file'" << std::endl;
182 return 2;
183 }
184 std::istringstream is(vm["app-params"].as<std::string>());
185 try {
186 options.applicationParameters = io::loadBuffer(is, io::BASE64);
187 }
188 catch (const io::Error& e) {
189 std::cerr << "ERROR: invalid ApplicationParameters string: " << e.what() << std::endl;
190 return 2;
191 }
192 }
193
194 if (vm.count("app-params-file") > 0) {
195 auto filename = vm["app-params-file"].as<std::string>();
196 std::ifstream paramsFile = openBinaryFile(filename);
197 if (!paramsFile) {
198 return 2;
199 }
200 try {
201 options.applicationParameters = io::loadBuffer(paramsFile, io::NO_ENCODING);
202 }
203 catch (const io::Error& e) {
204 std::cerr << "ERROR: cannot read ApplicationParameters from file '" << filename
205 << "': " << e.what() << std::endl;
206 return 2;
207 }
208 }
209
Zhuo Lib3558892016-08-12 15:51:12 -0700210 try {
Davide Pesavento65d11552019-06-09 19:15:50 -0400211 Face face;
212 NdnPeek program(face, options);
213
Zhuo Lib3558892016-08-12 15:51:12 -0700214 program.start();
Davide Pesavento65d11552019-06-09 19:15:50 -0400215 face.processEvents();
216
Davide Pesavento87434be2019-07-25 19:04:23 -0400217 return static_cast<int>(program.getResult());
Zhuo Lib3558892016-08-12 15:51:12 -0700218 }
219 catch (const std::exception& e) {
220 std::cerr << "ERROR: " << e.what() << std::endl;
221 return 1;
222 }
Zhuo Lib3558892016-08-12 15:51:12 -0700223}
224
225} // namespace peek
226} // namespace ndn
227
228int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400229main(int argc, char* argv[])
Zhuo Lib3558892016-08-12 15:51:12 -0700230{
231 return ndn::peek::main(argc, argv);
232}