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