blob: fb26ab0f6d905b4f2e1fb154be7a8c7238bb1912 [file] [log] [blame]
Eric Newberry2f041d22018-06-03 18:02:31 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoaacc7da2019-03-15 19:42:24 -04003 * Copyright (c) 2014-2019, 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>
26 */
27
28#include "ndnpoke.hpp"
29#include "core/version.hpp"
30
Eric Newberry2f041d22018-06-03 18:02:31 -070031namespace ndn {
32namespace peek {
33
34namespace po = boost::program_options;
35
36static void
Davide Pesaventoe75861e2019-07-24 21:55:39 -040037usage(std::ostream& os, const std::string& program, const po::options_description& options)
Eric Newberry2f041d22018-06-03 18:02:31 -070038{
Davide Pesaventoe75861e2019-07-24 21:55:39 -040039 os << "Usage: " << program << " [options] /name\n"
40 << "\n"
41 << "Reads a payload from the standard input and sends it as a single Data packet.\n"
Eric Newberry2f041d22018-06-03 18:02:31 -070042 << options;
43}
44
45static int
46main(int argc, char* argv[])
47{
Davide Pesaventoe75861e2019-07-24 21:55:39 -040048 std::string progName(argv[0]);
Eric Newberry2f041d22018-06-03 18:02:31 -070049 PokeOptions options;
Davide Pesaventoe75861e2019-07-24 21:55:39 -040050 bool wantDigestSha256 = false;
Eric Newberry2f041d22018-06-03 18:02:31 -070051
52 po::options_description visibleOptDesc;
53 visibleOptDesc.add_options()
Davide Pesaventoe75861e2019-07-24 21:55:39 -040054 ("help,h", "print help and exit")
55 ("force,f", po::bool_switch(&options.wantForceData),
56 "send the Data packet without waiting for an incoming Interest")
57 ("final,F", po::bool_switch(&options.wantFinalBlockId),
58 "set FinalBlockId to the last component of the Data name")
59 ("freshness,x", po::value<int>(), "set FreshnessPeriod (in milliseconds)")
60 ("identity,i", po::value<std::string>(), "use the specified identity for signing")
61 ("digest,D", po::bool_switch(&wantDigestSha256),
62 "use DigestSha256 signing method instead of SignatureSha256WithRsa")
63 ("timeout,w", po::value<int>(), "set timeout (in milliseconds)")
64 ("version,V", "print version and exit")
Eric Newberry2f041d22018-06-03 18:02:31 -070065 ;
66
67 po::options_description hiddenOptDesc;
68 hiddenOptDesc.add_options()
69 ("name", po::value<std::string>(), "Data name");
70
71 po::options_description optDesc;
72 optDesc.add(visibleOptDesc).add(hiddenOptDesc);
73
74 po::positional_options_description optPos;
75 optPos.add("name", -1);
76
77 po::variables_map vm;
78 try {
79 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm);
80 po::notify(vm);
81 }
82 catch (const po::error& e) {
83 std::cerr << "ERROR: " << e.what() << std::endl;
84 return 2;
85 }
86
Eric Newberry2f041d22018-06-03 18:02:31 -070087 if (vm.count("help") > 0) {
Davide Pesaventoe75861e2019-07-24 21:55:39 -040088 usage(std::cout, progName, visibleOptDesc);
Eric Newberry2f041d22018-06-03 18:02:31 -070089 return 0;
90 }
91
92 if (vm.count("version") > 0) {
93 std::cout << "ndnpoke " << tools::VERSION << std::endl;
94 return 0;
95 }
96
Davide Pesaventoe75861e2019-07-24 21:55:39 -040097 if (vm.count("name") == 0) {
98 std::cerr << "ERROR: missing name\n\n";
99 usage(std::cerr, progName, visibleOptDesc);
Eric Newberry2f041d22018-06-03 18:02:31 -0700100 return 2;
101 }
102
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400103 try {
104 options.name = vm["name"].as<std::string>();
105 }
106 catch (const Name::Error& e) {
107 std::cerr << "ERROR: invalid name: " << e.what() << std::endl;
108 return 2;
109 }
110
111 if (options.name.empty()) {
112 std::cerr << "ERROR: name cannot have zero components" << std::endl;
113 return 2;
114 }
115
116 if (vm.count("freshness") > 0) {
117 if (vm["freshness"].as<int>() < 0) {
118 std::cerr << "ERROR: freshness cannot be negative" << std::endl;
119 return 2;
120 }
121 options.freshnessPeriod = time::milliseconds(vm["freshness"].as<int>());
122 }
123
124 if (vm.count("identity") > 0) {
125 if (wantDigestSha256) {
126 std::cerr << "ERROR: conflicting '--digest' and '--identity' options specified" << std::endl;
127 return 2;
128 }
129 try {
130 options.signingInfo.setSigningIdentity(vm["identity"].as<std::string>());
131 }
132 catch (const Name::Error& e) {
133 std::cerr << "ERROR: invalid identity name: " << e.what() << std::endl;
134 return 2;
135 }
136 }
137
Eric Newberry2f041d22018-06-03 18:02:31 -0700138 if (wantDigestSha256) {
139 options.signingInfo.setSha256Signing();
140 }
141
Eric Newberry2f041d22018-06-03 18:02:31 -0700142 if (vm.count("timeout") > 0) {
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400143 if (options.wantForceData) {
144 std::cerr << "ERROR: conflicting '--force' and '--timeout' options specified" << std::endl;
Eric Newberry2f041d22018-06-03 18:02:31 -0700145 return 2;
146 }
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400147 if (vm["timeout"].as<int>() < 0) {
148 std::cerr << "ERROR: timeout cannot be negative" << std::endl;
149 return 2;
150 }
151 options.timeout = time::milliseconds(vm["timeout"].as<int>());
Eric Newberry2f041d22018-06-03 18:02:31 -0700152 }
153
Eric Newberry2f041d22018-06-03 18:02:31 -0700154 try {
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400155 Face face;
156 KeyChain keyChain;
157 NdnPoke program(face, keyChain, std::cin, options);
158
Eric Newberry2f041d22018-06-03 18:02:31 -0700159 program.start();
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400160 face.processEvents();
161
Davide Pesavento87434be2019-07-25 19:04:23 -0400162 return static_cast<int>(program.getResult());
Eric Newberry2f041d22018-06-03 18:02:31 -0700163 }
164 catch (const std::exception& e) {
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400165 std::cerr << "ERROR: " << e.what() << std::endl;
Eric Newberry2f041d22018-06-03 18:02:31 -0700166 return 1;
167 }
168}
169
170} // namespace peek
171} // namespace ndn
172
173int
174main(int argc, char* argv[])
175{
176 return ndn::peek::main(argc, argv);
177}