Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2021, Regents of the University of California, |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 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> |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 26 | * @author Davide Pesavento <davidepesa@gmail.com> |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #include "ndnpoke.hpp" |
| 30 | #include "core/version.hpp" |
| 31 | |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame^] | 32 | #include <boost/program_options/options_description.hpp> |
| 33 | #include <boost/program_options/parsers.hpp> |
| 34 | #include <boost/program_options/variables_map.hpp> |
| 35 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 36 | namespace ndn { |
| 37 | namespace peek { |
| 38 | |
| 39 | namespace po = boost::program_options; |
| 40 | |
| 41 | static void |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 42 | usage(std::ostream& os, const std::string& program, const po::options_description& options) |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 43 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 44 | os << "Usage: " << program << " [options] /name\n" |
| 45 | << "\n" |
| 46 | << "Reads a payload from the standard input and sends it as a single Data packet.\n" |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 47 | << options; |
| 48 | } |
| 49 | |
| 50 | static int |
| 51 | main(int argc, char* argv[]) |
| 52 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 53 | std::string progName(argv[0]); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 54 | PokeOptions options; |
Davide Pesavento | 7de32c1 | 2019-07-26 20:08:21 -0400 | [diff] [blame] | 55 | std::string signingStr; |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 56 | bool wantDigestSha256 = false; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 57 | |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 58 | 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 Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 63 | ("timeout,w", po::value<time::milliseconds::rep>(), "execution timeout, in milliseconds") |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 64 | ("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 Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 70 | ("final,F", po::bool_switch(&options.wantFinalBlockId), |
| 71 | "set FinalBlockId to the last component of the Data name") |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 72 | ("freshness,x", po::value<time::milliseconds::rep>()->default_value(options.freshnessPeriod.count()), |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 73 | "set FreshnessPeriod, in milliseconds") |
Davide Pesavento | 7de32c1 | 2019-07-26 20:08:21 -0400 | [diff] [blame] | 74 | ("signing-info,S", po::value<std::string>(&signingStr), "see 'man ndnpoke' for usage") |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 75 | ; |
| 76 | |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 77 | po::options_description visibleOptDesc; |
| 78 | visibleOptDesc.add(genericOptDesc).add(dataOptDesc); |
| 79 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 80 | po::options_description hiddenOptDesc; |
| 81 | hiddenOptDesc.add_options() |
| 82 | ("name", po::value<std::string>(), "Data name"); |
| 83 | |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 84 | po::options_description deprecatedOptDesc; |
| 85 | deprecatedOptDesc.add_options() |
Davide Pesavento | 7de32c1 | 2019-07-26 20:08:21 -0400 | [diff] [blame] | 86 | ("force,f", po::bool_switch()) |
| 87 | ("identity,i", po::value<std::string>()) |
| 88 | ("digest,D", po::bool_switch(&wantDigestSha256)) |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 89 | ; |
| 90 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 91 | po::options_description optDesc; |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 92 | optDesc.add(visibleOptDesc).add(hiddenOptDesc).add(deprecatedOptDesc); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 93 | |
| 94 | po::positional_options_description optPos; |
| 95 | optPos.add("name", -1); |
| 96 | |
| 97 | po::variables_map vm; |
| 98 | try { |
| 99 | po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm); |
| 100 | po::notify(vm); |
| 101 | } |
| 102 | catch (const po::error& e) { |
| 103 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 104 | return 2; |
| 105 | } |
| 106 | |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 107 | if (vm["force"].as<bool>()) { |
| 108 | std::cerr << "WARNING: option '-f/--force' is deprecated and will be removed " |
| 109 | "in the near future. Use '-u/--unsolicited' instead." << std::endl; |
| 110 | options.wantUnsolicited = true; |
| 111 | } |
Davide Pesavento | 7de32c1 | 2019-07-26 20:08:21 -0400 | [diff] [blame] | 112 | if (wantDigestSha256 || vm.count("identity") > 0) { |
| 113 | std::cerr << "WARNING: options '-i/--identity' and '-D/--digest' are deprecated and will be " |
| 114 | "removed in the near future. Use '-S/--signing-info' instead." << std::endl; |
| 115 | } |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 116 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 117 | if (vm.count("help") > 0) { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 118 | usage(std::cout, progName, visibleOptDesc); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | if (vm.count("version") > 0) { |
| 123 | std::cout << "ndnpoke " << tools::VERSION << std::endl; |
| 124 | return 0; |
| 125 | } |
| 126 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 127 | if (vm.count("name") == 0) { |
| 128 | std::cerr << "ERROR: missing name\n\n"; |
| 129 | usage(std::cerr, progName, visibleOptDesc); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 130 | return 2; |
| 131 | } |
| 132 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 133 | try { |
| 134 | options.name = vm["name"].as<std::string>(); |
| 135 | } |
| 136 | catch (const Name::Error& e) { |
| 137 | std::cerr << "ERROR: invalid name: " << e.what() << std::endl; |
| 138 | return 2; |
| 139 | } |
| 140 | |
| 141 | if (options.name.empty()) { |
| 142 | std::cerr << "ERROR: name cannot have zero components" << std::endl; |
| 143 | return 2; |
| 144 | } |
| 145 | |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 146 | options.freshnessPeriod = time::milliseconds(vm["freshness"].as<time::milliseconds::rep>()); |
| 147 | if (options.freshnessPeriod < 0_ms) { |
| 148 | std::cerr << "ERROR: freshness cannot be negative" << std::endl; |
| 149 | return 2; |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if (vm.count("identity") > 0) { |
| 153 | if (wantDigestSha256) { |
| 154 | std::cerr << "ERROR: conflicting '--digest' and '--identity' options specified" << std::endl; |
| 155 | return 2; |
| 156 | } |
| 157 | try { |
| 158 | options.signingInfo.setSigningIdentity(vm["identity"].as<std::string>()); |
| 159 | } |
| 160 | catch (const Name::Error& e) { |
| 161 | std::cerr << "ERROR: invalid identity name: " << e.what() << std::endl; |
| 162 | return 2; |
| 163 | } |
| 164 | } |
| 165 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 166 | if (wantDigestSha256) { |
| 167 | options.signingInfo.setSha256Signing(); |
| 168 | } |
| 169 | |
Davide Pesavento | 7de32c1 | 2019-07-26 20:08:21 -0400 | [diff] [blame] | 170 | try { |
| 171 | options.signingInfo = security::SigningInfo(signingStr); |
| 172 | } |
| 173 | catch (const std::invalid_argument& e) { |
| 174 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 175 | return 2; |
| 176 | } |
| 177 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 178 | if (vm.count("timeout") > 0) { |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 179 | if (options.wantUnsolicited) { |
| 180 | std::cerr << "ERROR: conflicting '--unsolicited' and '--timeout' options specified" << std::endl; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 181 | return 2; |
| 182 | } |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 183 | options.timeout = time::milliseconds(vm["timeout"].as<time::milliseconds::rep>()); |
| 184 | if (*options.timeout < 0_ms) { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 185 | std::cerr << "ERROR: timeout cannot be negative" << std::endl; |
| 186 | return 2; |
| 187 | } |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 190 | try { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 191 | Face face; |
| 192 | KeyChain keyChain; |
| 193 | NdnPoke program(face, keyChain, std::cin, options); |
| 194 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 195 | program.start(); |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 196 | face.processEvents(); |
| 197 | |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 198 | return static_cast<int>(program.getResult()); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 199 | } |
| 200 | catch (const std::exception& e) { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 201 | std::cerr << "ERROR: " << e.what() << std::endl; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 202 | return 1; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | } // namespace peek |
| 207 | } // namespace ndn |
| 208 | |
| 209 | int |
| 210 | main(int argc, char* argv[]) |
| 211 | { |
| 212 | return ndn::peek::main(argc, argv); |
| 213 | } |