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 | aacc7da | 2019-03-15 19:42:24 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 32 | namespace ndn { |
| 33 | namespace peek { |
| 34 | |
| 35 | namespace po = boost::program_options; |
| 36 | |
| 37 | static void |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 38 | 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] | 39 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 40 | os << "Usage: " << program << " [options] /name\n" |
| 41 | << "\n" |
| 42 | << "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] | 43 | << options; |
| 44 | } |
| 45 | |
| 46 | static int |
| 47 | main(int argc, char* argv[]) |
| 48 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 49 | std::string progName(argv[0]); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 50 | PokeOptions options; |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 51 | bool wantDigestSha256 = false; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 52 | |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 53 | po::options_description genericOptDesc("Generic options"); |
| 54 | genericOptDesc.add_options() |
| 55 | ("help,h", "print help and exit") |
| 56 | ("unsolicited,u", po::bool_switch(&options.wantUnsolicited), |
| 57 | "send the Data packet without waiting for an incoming Interest") |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame^] | 58 | ("timeout,w", po::value<time::milliseconds::rep>(), "execution timeout, in milliseconds") |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 59 | ("verbose,v", po::bool_switch(&options.isVerbose), "turn on verbose output") |
| 60 | ("version,V", "print version and exit") |
| 61 | ; |
| 62 | |
| 63 | po::options_description dataOptDesc("Data construction"); |
| 64 | dataOptDesc.add_options() |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 65 | ("final,F", po::bool_switch(&options.wantFinalBlockId), |
| 66 | "set FinalBlockId to the last component of the Data name") |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 67 | ("freshness,x", po::value<time::milliseconds::rep>()->default_value(options.freshnessPeriod.count()), |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame^] | 68 | "set FreshnessPeriod, in milliseconds") |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 69 | ("identity,i", po::value<std::string>(), "use the specified identity for signing") |
| 70 | ("digest,D", po::bool_switch(&wantDigestSha256), |
| 71 | "use DigestSha256 signing method instead of SignatureSha256WithRsa") |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 72 | ; |
| 73 | |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 74 | po::options_description visibleOptDesc; |
| 75 | visibleOptDesc.add(genericOptDesc).add(dataOptDesc); |
| 76 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 77 | po::options_description hiddenOptDesc; |
| 78 | hiddenOptDesc.add_options() |
| 79 | ("name", po::value<std::string>(), "Data name"); |
| 80 | |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 81 | po::options_description deprecatedOptDesc; |
| 82 | deprecatedOptDesc.add_options() |
| 83 | ("force,f", po::bool_switch()) |
| 84 | ; |
| 85 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 86 | po::options_description optDesc; |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 87 | optDesc.add(visibleOptDesc).add(hiddenOptDesc).add(deprecatedOptDesc); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 88 | |
| 89 | po::positional_options_description optPos; |
| 90 | optPos.add("name", -1); |
| 91 | |
| 92 | po::variables_map vm; |
| 93 | try { |
| 94 | po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm); |
| 95 | po::notify(vm); |
| 96 | } |
| 97 | catch (const po::error& e) { |
| 98 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 99 | return 2; |
| 100 | } |
| 101 | |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 102 | if (vm["force"].as<bool>()) { |
| 103 | std::cerr << "WARNING: option '-f/--force' is deprecated and will be removed " |
| 104 | "in the near future. Use '-u/--unsolicited' instead." << std::endl; |
| 105 | options.wantUnsolicited = true; |
| 106 | } |
| 107 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 108 | if (vm.count("help") > 0) { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 109 | usage(std::cout, progName, visibleOptDesc); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | if (vm.count("version") > 0) { |
| 114 | std::cout << "ndnpoke " << tools::VERSION << std::endl; |
| 115 | return 0; |
| 116 | } |
| 117 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 118 | if (vm.count("name") == 0) { |
| 119 | std::cerr << "ERROR: missing name\n\n"; |
| 120 | usage(std::cerr, progName, visibleOptDesc); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 121 | return 2; |
| 122 | } |
| 123 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 124 | try { |
| 125 | options.name = vm["name"].as<std::string>(); |
| 126 | } |
| 127 | catch (const Name::Error& e) { |
| 128 | std::cerr << "ERROR: invalid name: " << e.what() << std::endl; |
| 129 | return 2; |
| 130 | } |
| 131 | |
| 132 | if (options.name.empty()) { |
| 133 | std::cerr << "ERROR: name cannot have zero components" << std::endl; |
| 134 | return 2; |
| 135 | } |
| 136 | |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame^] | 137 | options.freshnessPeriod = time::milliseconds(vm["freshness"].as<time::milliseconds::rep>()); |
| 138 | if (options.freshnessPeriod < 0_ms) { |
| 139 | std::cerr << "ERROR: freshness cannot be negative" << std::endl; |
| 140 | return 2; |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | if (vm.count("identity") > 0) { |
| 144 | if (wantDigestSha256) { |
| 145 | std::cerr << "ERROR: conflicting '--digest' and '--identity' options specified" << std::endl; |
| 146 | return 2; |
| 147 | } |
| 148 | try { |
| 149 | options.signingInfo.setSigningIdentity(vm["identity"].as<std::string>()); |
| 150 | } |
| 151 | catch (const Name::Error& e) { |
| 152 | std::cerr << "ERROR: invalid identity name: " << e.what() << std::endl; |
| 153 | return 2; |
| 154 | } |
| 155 | } |
| 156 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 157 | if (wantDigestSha256) { |
| 158 | options.signingInfo.setSha256Signing(); |
| 159 | } |
| 160 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 161 | if (vm.count("timeout") > 0) { |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 162 | if (options.wantUnsolicited) { |
| 163 | std::cerr << "ERROR: conflicting '--unsolicited' and '--timeout' options specified" << std::endl; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 164 | return 2; |
| 165 | } |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame^] | 166 | options.timeout = time::milliseconds(vm["timeout"].as<time::milliseconds::rep>()); |
| 167 | if (*options.timeout < 0_ms) { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 168 | std::cerr << "ERROR: timeout cannot be negative" << std::endl; |
| 169 | return 2; |
| 170 | } |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 173 | try { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 174 | Face face; |
| 175 | KeyChain keyChain; |
| 176 | NdnPoke program(face, keyChain, std::cin, options); |
| 177 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 178 | program.start(); |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 179 | face.processEvents(); |
| 180 | |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 181 | return static_cast<int>(program.getResult()); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 182 | } |
| 183 | catch (const std::exception& e) { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 184 | std::cerr << "ERROR: " << e.what() << std::endl; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 185 | return 1; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | } // namespace peek |
| 190 | } // namespace ndn |
| 191 | |
| 192 | int |
| 193 | main(int argc, char* argv[]) |
| 194 | { |
| 195 | return ndn::peek::main(argc, argv); |
| 196 | } |