Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Junxiao Shi | 1688ded | 2015-03-29 10:09:26 -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/>. |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 24 | * |
| 25 | * @author Jerald Paul Abraham <jeraldabraham@email.arizona.edu> |
| 26 | */ |
| 27 | |
Junxiao Shi | 1753afb | 2015-04-17 20:59:50 -0700 | [diff] [blame] | 28 | #include "core/version.hpp" |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 29 | |
Davide Pesavento | 60f8cc1 | 2018-05-10 22:05:21 -0400 | [diff] [blame^] | 30 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 31 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 32 | #include <sstream> |
| 33 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 34 | namespace ndn { |
| 35 | namespace peek { |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 36 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 37 | class NdnPoke : boost::noncopyable |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 38 | { |
| 39 | public: |
| 40 | explicit |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 41 | NdnPoke(const char* programName) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 42 | : m_programName(programName) |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 43 | , m_wantForceData(false) |
| 44 | , m_wantDigestSha256(false) |
| 45 | , m_wantFinalBlockId(false) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 46 | , m_freshnessPeriod(-1) |
| 47 | , m_timeout(-1) |
| 48 | , m_isDataSent(false) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | usage() |
| 54 | { |
| 55 | std::cout << "\n Usage:\n " << m_programName << " " |
| 56 | "[-f] [-D] [-i identity] [-F] [-x freshness] [-w timeout] ndn:/name\n" |
| 57 | " Reads payload from stdin and sends it to local NDN forwarder as a " |
| 58 | "single Data packet\n" |
| 59 | " [-f] - force, send Data without waiting for Interest\n" |
| 60 | " [-D] - use DigestSha256 signing method instead of " |
| 61 | "SignatureSha256WithRsa\n" |
| 62 | " [-i identity] - set identity to be used for signing\n" |
| 63 | " [-F] - set FinalBlockId to the last component of Name\n" |
| 64 | " [-x] - set FreshnessPeriod in time::milliseconds\n" |
| 65 | " [-w timeout] - set Timeout in time::milliseconds\n" |
| 66 | " [-h] - print help and exit\n" |
| 67 | " [-V] - print version and exit\n" |
| 68 | "\n"; |
| 69 | exit(1); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | setForceData() |
| 74 | { |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 75 | m_wantForceData = true; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void |
| 79 | setUseDigestSha256() |
| 80 | { |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 81 | m_wantDigestSha256 = true; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 85 | setIdentityName(const char* identityName) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 86 | { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 87 | m_identityName = make_shared<Name>(identityName); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void |
| 91 | setLastAsFinalBlockId() |
| 92 | { |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 93 | m_wantFinalBlockId = true; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void |
| 97 | setFreshnessPeriod(int freshnessPeriod) |
| 98 | { |
| 99 | if (freshnessPeriod < 0) |
| 100 | usage(); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 101 | |
| 102 | m_freshnessPeriod = time::milliseconds(freshnessPeriod); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void |
| 106 | setTimeout(int timeout) |
| 107 | { |
| 108 | if (timeout < 0) |
| 109 | usage(); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 110 | |
| 111 | m_timeout = time::milliseconds(timeout); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 115 | setPrefixName(const char* prefixName) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 116 | { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 117 | m_prefixName = Name(prefixName); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 120 | time::milliseconds |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 121 | getDefaultTimeout() |
| 122 | { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 123 | return time::seconds(10); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Junxiao Shi | 7348ee7 | 2015-06-07 20:57:34 -0700 | [diff] [blame] | 126 | shared_ptr<Data> |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 127 | createDataPacket() |
| 128 | { |
Junxiao Shi | 7348ee7 | 2015-06-07 20:57:34 -0700 | [diff] [blame] | 129 | auto dataPacket = make_shared<Data>(m_prefixName); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 130 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 131 | std::stringstream payloadStream; |
| 132 | payloadStream << std::cin.rdbuf(); |
| 133 | std::string payload = payloadStream.str(); |
Junxiao Shi | 7348ee7 | 2015-06-07 20:57:34 -0700 | [diff] [blame] | 134 | dataPacket->setContent(reinterpret_cast<const uint8_t*>(payload.c_str()), payload.length()); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 135 | |
| 136 | if (m_freshnessPeriod >= time::milliseconds::zero()) |
Junxiao Shi | 7348ee7 | 2015-06-07 20:57:34 -0700 | [diff] [blame] | 137 | dataPacket->setFreshnessPeriod(m_freshnessPeriod); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 138 | |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 139 | if (m_wantFinalBlockId) { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 140 | if (!m_prefixName.empty()) |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 141 | dataPacket->setFinalBlock(m_prefixName.get(-1)); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 142 | else { |
| 143 | std::cerr << "Name Provided Has 0 Components" << std::endl; |
| 144 | exit(1); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 145 | } |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 148 | if (m_wantDigestSha256) { |
Eric Newberry | 38146c5 | 2015-06-22 15:11:30 -0700 | [diff] [blame] | 149 | m_keyChain.sign(*dataPacket, signingWithSha256()); |
| 150 | } |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 151 | else { |
Eric Newberry | 38146c5 | 2015-06-22 15:11:30 -0700 | [diff] [blame] | 152 | if (m_identityName == nullptr) { |
Junxiao Shi | 7348ee7 | 2015-06-07 20:57:34 -0700 | [diff] [blame] | 153 | m_keyChain.sign(*dataPacket); |
Eric Newberry | 38146c5 | 2015-06-22 15:11:30 -0700 | [diff] [blame] | 154 | } |
| 155 | else { |
| 156 | m_keyChain.sign(*dataPacket, signingByIdentity(*m_identityName)); |
| 157 | } |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 160 | return dataPacket; |
| 161 | } |
| 162 | |
| 163 | void |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 164 | onInterest(const Name& name, const Interest& interest, const shared_ptr<Data>& data) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 165 | { |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 166 | m_face.put(*data); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 167 | m_isDataSent = true; |
| 168 | m_face.shutdown(); |
| 169 | } |
| 170 | |
| 171 | void |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 172 | onRegisterFailed(const Name& prefix, const std::string& reason) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 173 | { |
| 174 | std::cerr << "Prefix Registration Failure." << std::endl; |
| 175 | std::cerr << "Reason = " << reason << std::endl; |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | run() |
| 180 | { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 181 | try { |
Junxiao Shi | 7348ee7 | 2015-06-07 20:57:34 -0700 | [diff] [blame] | 182 | shared_ptr<Data> dataPacket = createDataPacket(); |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 183 | if (m_wantForceData) { |
Junxiao Shi | 7348ee7 | 2015-06-07 20:57:34 -0700 | [diff] [blame] | 184 | m_face.put(*dataPacket); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 185 | m_isDataSent = true; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 186 | } |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 187 | else { |
| 188 | m_face.setInterestFilter(m_prefixName, |
| 189 | bind(&NdnPoke::onInterest, this, _1, _2, dataPacket), |
| 190 | RegisterPrefixSuccessCallback(), |
| 191 | bind(&NdnPoke::onRegisterFailed, this, _1, _2)); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 192 | } |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 193 | |
| 194 | if (m_timeout < time::milliseconds::zero()) |
| 195 | m_face.processEvents(getDefaultTimeout()); |
| 196 | else |
| 197 | m_face.processEvents(m_timeout); |
| 198 | } |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 199 | catch (const std::exception& e) { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 200 | std::cerr << "ERROR: " << e.what() << "\n" << std::endl; |
| 201 | exit(1); |
| 202 | } |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | bool |
| 206 | isDataSent() const |
| 207 | { |
| 208 | return m_isDataSent; |
| 209 | } |
| 210 | |
| 211 | private: |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 212 | KeyChain m_keyChain; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 213 | std::string m_programName; |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 214 | bool m_wantForceData; |
| 215 | bool m_wantDigestSha256; |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 216 | shared_ptr<Name> m_identityName; |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 217 | bool m_wantFinalBlockId; |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 218 | time::milliseconds m_freshnessPeriod; |
| 219 | time::milliseconds m_timeout; |
| 220 | Name m_prefixName; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 221 | bool m_isDataSent; |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 222 | Face m_face; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 223 | }; |
| 224 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 225 | int |
| 226 | main(int argc, char* argv[]) |
| 227 | { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 228 | NdnPoke program(argv[0]); |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 229 | |
| 230 | int option; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 231 | while ((option = getopt(argc, argv, "hfDi:Fx:w:V")) != -1) { |
| 232 | switch (option) { |
| 233 | case 'h': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 234 | program.usage(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 235 | break; |
| 236 | case 'f': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 237 | program.setForceData(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 238 | break; |
| 239 | case 'D': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 240 | program.setUseDigestSha256(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 241 | break; |
| 242 | case 'i': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 243 | program.setIdentityName(optarg); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 244 | break; |
| 245 | case 'F': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 246 | program.setLastAsFinalBlockId(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 247 | break; |
| 248 | case 'x': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 249 | program.setFreshnessPeriod(atoi(optarg)); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 250 | break; |
| 251 | case 'w': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 252 | program.setTimeout(atoi(optarg)); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 253 | break; |
| 254 | case 'V': |
Junxiao Shi | 1753afb | 2015-04-17 20:59:50 -0700 | [diff] [blame] | 255 | std::cout << "ndnpoke " << tools::VERSION << std::endl; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 256 | return 0; |
| 257 | default: |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 258 | program.usage(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 259 | break; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | argc -= optind; |
| 264 | argv += optind; |
| 265 | |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 266 | if (!argv[0]) |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 267 | program.usage(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 268 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 269 | program.setPrefixName(argv[0]); |
| 270 | program.run(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 271 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 272 | if (program.isDataSent()) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 273 | return 0; |
| 274 | else |
| 275 | return 1; |
| 276 | } |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 277 | |
| 278 | } // namespace peek |
| 279 | } // namespace ndn |
| 280 | |
| 281 | int |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 282 | main(int argc, char* argv[]) |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 283 | { |
| 284 | return ndn::peek::main(argc, argv); |
| 285 | } |