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> |
| 26 | */ |
| 27 | |
| 28 | #include "ndnpoke.hpp" |
| 29 | #include "core/version.hpp" |
| 30 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 31 | namespace ndn { |
| 32 | namespace peek { |
| 33 | |
| 34 | namespace po = boost::program_options; |
| 35 | |
| 36 | static void |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 37 | 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] | 38 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 39 | 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 Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 42 | << options; |
| 43 | } |
| 44 | |
| 45 | static int |
| 46 | main(int argc, char* argv[]) |
| 47 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 48 | std::string progName(argv[0]); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 49 | PokeOptions options; |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 50 | bool wantDigestSha256 = false; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 51 | |
| 52 | po::options_description visibleOptDesc; |
| 53 | visibleOptDesc.add_options() |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 54 | ("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 Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 65 | ; |
| 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 Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 87 | if (vm.count("help") > 0) { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 88 | usage(std::cout, progName, visibleOptDesc); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | if (vm.count("version") > 0) { |
| 93 | std::cout << "ndnpoke " << tools::VERSION << std::endl; |
| 94 | return 0; |
| 95 | } |
| 96 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 97 | if (vm.count("name") == 0) { |
| 98 | std::cerr << "ERROR: missing name\n\n"; |
| 99 | usage(std::cerr, progName, visibleOptDesc); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 100 | return 2; |
| 101 | } |
| 102 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 103 | 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 Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 138 | if (wantDigestSha256) { |
| 139 | options.signingInfo.setSha256Signing(); |
| 140 | } |
| 141 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 142 | if (vm.count("timeout") > 0) { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 143 | if (options.wantForceData) { |
| 144 | std::cerr << "ERROR: conflicting '--force' and '--timeout' options specified" << std::endl; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 145 | return 2; |
| 146 | } |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 147 | 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 Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 154 | try { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 155 | Face face; |
| 156 | KeyChain keyChain; |
| 157 | NdnPoke program(face, keyChain, std::cin, options); |
| 158 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 159 | program.start(); |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 160 | face.processEvents(); |
| 161 | |
| 162 | return program.didSendData() ? 0 : 1; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 163 | } |
| 164 | catch (const std::exception& e) { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 165 | std::cerr << "ERROR: " << e.what() << std::endl; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 166 | return 1; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | } // namespace peek |
| 171 | } // namespace ndn |
| 172 | |
| 173 | int |
| 174 | main(int argc, char* argv[]) |
| 175 | { |
| 176 | return ndn::peek::main(argc, argv); |
| 177 | } |