blob: 5cb0dda048c43faef8dfc3ae175bc667a89a6046 [file] [log] [blame]
Eric Newberry2f041d22018-06-03 18:02:31 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2018, 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 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
31#include <ndn-cxx/util/io.hpp>
32
33#include <sstream>
34
35namespace ndn {
36namespace peek {
37
38namespace po = boost::program_options;
39
40static void
41usage(std::ostream& os, const po::options_description& options)
42{
43 os << "Usage: ndnpoke [options] ndn:/name\n"
44 "\n"
45 "Reads payload from stdin and sends it to the local NDN forwarder as a single Data packet\n"
46 "\n"
47 << options;
48}
49
50static int
51main(int argc, char* argv[])
52{
53 PokeOptions options;
54 bool wantDigestSha256;
55
56 po::options_description visibleOptDesc;
57 visibleOptDesc.add_options()
58 ("help,h", "print help and exit")
59 ("version,V", "print version and exit")
60 ("force,f", po::bool_switch(&options.wantForceData),
61 "for, send Data without waiting for Interest")
62 ("digest,D", po::bool_switch(&wantDigestSha256),
63 "use DigestSha256 signing method instead of SignatureSha256WithRsa")
64 ("identity,i", po::value<std::string>(),
65 "set identity to be used for signing")
66 ("final,F", po::bool_switch(&options.wantLastAsFinalBlockId),
67 "set FinalBlockId to the last component of Name")
68 ("freshness,x", po::value<int>(),
69 "set FreshnessPeriod in milliseconds")
70 ("timeout,w", po::value<int>(),
71 "set Timeout in milliseconds")
72 ;
73
74 po::options_description hiddenOptDesc;
75 hiddenOptDesc.add_options()
76 ("name", po::value<std::string>(), "Data name");
77
78 po::options_description optDesc;
79 optDesc.add(visibleOptDesc).add(hiddenOptDesc);
80
81 po::positional_options_description optPos;
82 optPos.add("name", -1);
83
84 po::variables_map vm;
85 try {
86 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm);
87 po::notify(vm);
88 }
89 catch (const po::error& e) {
90 std::cerr << "ERROR: " << e.what() << std::endl;
91 return 2;
92 }
93
94 // We store timeout here, instead of PokeOptions, because processEvents is called outside the NdnPoke class
95 time::milliseconds timeout = 10_s;
96
97 if (vm.count("help") > 0) {
98 usage(std::cout, visibleOptDesc);
99 return 0;
100 }
101
102 if (vm.count("version") > 0) {
103 std::cout << "ndnpoke " << tools::VERSION << std::endl;
104 return 0;
105 }
106
107 if (vm.count("name") > 0) {
108 options.prefixName = vm["name"].as<std::string>();
109 }
110 else {
111 std::cerr << "ERROR: Data name is missing" << std::endl;
112 usage(std::cerr, visibleOptDesc);
113 return 2;
114 }
115
116 if (wantDigestSha256) {
117 options.signingInfo.setSha256Signing();
118 }
119
120 if (vm.count("identity") > 0) {
121 if (wantDigestSha256) {
122 std::cerr << "ERROR: Signing identity cannot be specified when using DigestSha256 signing method" << std::endl;
123 usage(std::cerr, visibleOptDesc);
124 return 2;
125 }
126 options.signingInfo.setSigningIdentity(vm["identity"].as<std::string>());
127 }
128
129 if (vm.count("final") > 0) {
130 if (!options.prefixName.empty()) {
131 options.wantLastAsFinalBlockId = true;
132 }
133 else {
134 std::cerr << "The provided Name must have 1 or more components to be used with FinalBlockId option" << std::endl;
135 usage(std::cerr, visibleOptDesc);
136 return 1;
137 }
138 }
139
140 if (vm.count("freshness") > 0) {
141 if (vm["freshness"].as<int>() >= 0) {
142 options.freshnessPeriod = time::milliseconds(vm["freshness"].as<int>());
143 }
144 else {
145 std::cerr << "ERROR: FreshnessPeriod must be a non-negative integer" << std::endl;
146 usage(std::cerr, visibleOptDesc);
147 return 2;
148 }
149 }
150
151 if (vm.count("timeout") > 0) {
152 if (vm["timeout"].as<int>() > 0) {
153 timeout = time::milliseconds(vm["timeout"].as<int>());
154 }
155 else {
156 std::cerr << "ERROR: Timeout must a positive integer" << std::endl;
157 usage(std::cerr, visibleOptDesc);
158 return 2;
159 }
160 }
161
162 boost::asio::io_service io;
163 Face face(io);
164 KeyChain keyChain;
165 scheduler::Scheduler scheduler(io);
166 NdnPoke program(face, keyChain, std::cin, options);
167 try {
168 program.afterFinish.connect([&scheduler, &face] {
169 scheduler.scheduleEvent(2_s, [&face] { face.shutdown(); });
170 });
171 program.start();
172 face.processEvents(timeout);
173 }
174 catch (const std::exception& e) {
175 std::cerr << "ERROR: " << e.what() << "\n" << std::endl;
176 return 1;
177 }
178
179 if (program.wasDataSent()) {
180 return 0;
181 }
182 else {
183 return 1;
184 }
185}
186
187} // namespace peek
188} // namespace ndn
189
190int
191main(int argc, char* argv[])
192{
193 return ndn::peek::main(argc, argv);
194}