blob: 4939d5198d88c5ebf6c28f333d4d4bdbdd47acfc [file] [log] [blame]
Eric Newberry2f041d22018-06-03 18:02:31 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento5748e822024-01-26 18:40:22 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Eric Newberry2f041d22018-06-03 18:02:31 -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>
Davide Pesavento6a1396e2019-07-26 15:03:28 -040026 * @author Davide Pesavento <davidepesa@gmail.com>
Eric Newberry2f041d22018-06-03 18:02:31 -070027 */
28
29#include "ndnpoke.hpp"
30#include "core/version.hpp"
31
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050032#include <boost/program_options/options_description.hpp>
33#include <boost/program_options/parsers.hpp>
34#include <boost/program_options/variables_map.hpp>
35
Davide Pesavento5748e822024-01-26 18:40:22 -050036#include <iostream>
37
Davide Pesaventob3570c62022-02-19 19:19:00 -050038namespace ndn::peek {
Eric Newberry2f041d22018-06-03 18:02:31 -070039
40namespace po = boost::program_options;
41
42static void
Davide Pesaventob3570c62022-02-19 19:19:00 -050043usage(std::ostream& os, std::string_view program, const po::options_description& options)
Eric Newberry2f041d22018-06-03 18:02:31 -070044{
Davide Pesaventoe75861e2019-07-24 21:55:39 -040045 os << "Usage: " << program << " [options] /name\n"
46 << "\n"
47 << "Reads a payload from the standard input and sends it as a single Data packet.\n"
Eric Newberry2f041d22018-06-03 18:02:31 -070048 << options;
49}
50
51static int
52main(int argc, char* argv[])
53{
Davide Pesaventoe75861e2019-07-24 21:55:39 -040054 std::string progName(argv[0]);
Eric Newberry2f041d22018-06-03 18:02:31 -070055 PokeOptions options;
Davide Pesavento7de32c12019-07-26 20:08:21 -040056 std::string signingStr;
Eric Newberry2f041d22018-06-03 18:02:31 -070057
Davide Pesavento6a1396e2019-07-26 15:03:28 -040058 po::options_description genericOptDesc("Generic options");
59 genericOptDesc.add_options()
60 ("help,h", "print help and exit")
61 ("unsolicited,u", po::bool_switch(&options.wantUnsolicited),
62 "send the Data packet without waiting for an incoming Interest")
Davide Pesavento0da1f442019-07-26 17:38:13 -040063 ("timeout,w", po::value<time::milliseconds::rep>(), "execution timeout, in milliseconds")
Davide Pesavento6a1396e2019-07-26 15:03:28 -040064 ("verbose,v", po::bool_switch(&options.isVerbose), "turn on verbose output")
65 ("version,V", "print version and exit")
66 ;
67
68 po::options_description dataOptDesc("Data construction");
69 dataOptDesc.add_options()
Davide Pesavento6929b432021-01-26 17:07:58 -050070 ("freshness,f", po::value<time::milliseconds::rep>()->default_value(options.freshnessPeriod.count()),
71 "set FreshnessPeriod, in milliseconds")
72 ("final,F", po::bool_switch(&options.wantFinalBlockId),
73 "set FinalBlockId to the last component of the Data name")
74 ("signing-info,S", po::value<std::string>(&signingStr), "see 'man ndnpoke' for usage")
Eric Newberry2f041d22018-06-03 18:02:31 -070075 ;
76
Davide Pesavento6a1396e2019-07-26 15:03:28 -040077 po::options_description visibleOptDesc;
78 visibleOptDesc.add(genericOptDesc).add(dataOptDesc);
79
Eric Newberry2f041d22018-06-03 18:02:31 -070080 po::options_description hiddenOptDesc;
81 hiddenOptDesc.add_options()
82 ("name", po::value<std::string>(), "Data name");
83
84 po::options_description optDesc;
Davide Pesavento814ad342021-01-26 14:36:11 -050085 optDesc.add(visibleOptDesc).add(hiddenOptDesc);
Eric Newberry2f041d22018-06-03 18:02:31 -070086
87 po::positional_options_description optPos;
88 optPos.add("name", -1);
89
90 po::variables_map vm;
91 try {
92 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm);
93 po::notify(vm);
94 }
95 catch (const po::error& e) {
96 std::cerr << "ERROR: " << e.what() << std::endl;
97 return 2;
98 }
99
Eric Newberry2f041d22018-06-03 18:02:31 -0700100 if (vm.count("help") > 0) {
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400101 usage(std::cout, progName, visibleOptDesc);
Eric Newberry2f041d22018-06-03 18:02:31 -0700102 return 0;
103 }
104
105 if (vm.count("version") > 0) {
106 std::cout << "ndnpoke " << tools::VERSION << std::endl;
107 return 0;
108 }
109
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400110 if (vm.count("name") == 0) {
111 std::cerr << "ERROR: missing name\n\n";
112 usage(std::cerr, progName, visibleOptDesc);
Eric Newberry2f041d22018-06-03 18:02:31 -0700113 return 2;
114 }
115
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400116 try {
117 options.name = vm["name"].as<std::string>();
118 }
119 catch (const Name::Error& e) {
120 std::cerr << "ERROR: invalid name: " << e.what() << std::endl;
121 return 2;
122 }
123
124 if (options.name.empty()) {
125 std::cerr << "ERROR: name cannot have zero components" << std::endl;
126 return 2;
127 }
128
Davide Pesavento0da1f442019-07-26 17:38:13 -0400129 options.freshnessPeriod = time::milliseconds(vm["freshness"].as<time::milliseconds::rep>());
130 if (options.freshnessPeriod < 0_ms) {
131 std::cerr << "ERROR: freshness cannot be negative" << std::endl;
132 return 2;
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400133 }
134
Davide Pesavento7de32c12019-07-26 20:08:21 -0400135 try {
136 options.signingInfo = security::SigningInfo(signingStr);
137 }
138 catch (const std::invalid_argument& e) {
139 std::cerr << "ERROR: " << e.what() << std::endl;
140 return 2;
141 }
142
Eric Newberry2f041d22018-06-03 18:02:31 -0700143 if (vm.count("timeout") > 0) {
Davide Pesavento6a1396e2019-07-26 15:03:28 -0400144 if (options.wantUnsolicited) {
145 std::cerr << "ERROR: conflicting '--unsolicited' and '--timeout' options specified" << std::endl;
Eric Newberry2f041d22018-06-03 18:02:31 -0700146 return 2;
147 }
Davide Pesavento0da1f442019-07-26 17:38:13 -0400148 options.timeout = time::milliseconds(vm["timeout"].as<time::milliseconds::rep>());
149 if (*options.timeout < 0_ms) {
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400150 std::cerr << "ERROR: timeout cannot be negative" << std::endl;
151 return 2;
152 }
Eric Newberry2f041d22018-06-03 18:02:31 -0700153 }
154
Eric Newberry2f041d22018-06-03 18:02:31 -0700155 try {
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400156 Face face;
157 KeyChain keyChain;
158 NdnPoke program(face, keyChain, std::cin, options);
159
Eric Newberry2f041d22018-06-03 18:02:31 -0700160 program.start();
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400161 face.processEvents();
162
Davide Pesavento87434be2019-07-25 19:04:23 -0400163 return static_cast<int>(program.getResult());
Eric Newberry2f041d22018-06-03 18:02:31 -0700164 }
165 catch (const std::exception& e) {
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400166 std::cerr << "ERROR: " << e.what() << std::endl;
Eric Newberry2f041d22018-06-03 18:02:31 -0700167 return 1;
168 }
169}
170
Davide Pesaventob3570c62022-02-19 19:19:00 -0500171} // namespace ndn::peek
Eric Newberry2f041d22018-06-03 18:02:31 -0700172
173int
174main(int argc, char* argv[])
175{
176 return ndn::peek::main(argc, argv);
177}