Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 1688ded | 2015-03-29 10:09:26 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 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 | /** |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 26 | * Copyright (c) 2014, Regents of the University of California, |
| 27 | * Arizona Board of Regents, |
| 28 | * Colorado State University, |
| 29 | * University Pierre & Marie Curie, Sorbonne University, |
| 30 | * Washington University in St. Louis, |
| 31 | * Beijing Institute of Technology, |
| 32 | * The University of Memphis |
| 33 | * |
| 34 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 35 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 36 | * |
| 37 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 38 | * of the GNU General Public License as published by the Free Software Foundation, |
| 39 | * either version 3 of the License, or (at your option) any later version. |
| 40 | * |
| 41 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 42 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 43 | * PURPOSE. See the GNU General Public License for more details. |
| 44 | * |
| 45 | * You should have received a copy of the GNU General Public License along with |
| 46 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 47 | * |
| 48 | * @author Jerald Paul Abraham <jeraldabraham@email.arizona.edu> |
| 49 | */ |
| 50 | |
Junxiao Shi | 1753afb | 2015-04-17 20:59:50 -0700 | [diff] [blame] | 51 | #include "core/version.hpp" |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 52 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 53 | namespace ndn { |
| 54 | namespace peek { |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 55 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 56 | class NdnPoke : boost::noncopyable |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 57 | { |
| 58 | public: |
| 59 | explicit |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 60 | NdnPoke(char* programName) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 61 | : m_programName(programName) |
| 62 | , m_isForceDataSet(false) |
| 63 | , m_isUseDigestSha256Set(false) |
| 64 | , m_isLastAsFinalBlockIdSet(false) |
| 65 | , m_freshnessPeriod(-1) |
| 66 | , m_timeout(-1) |
| 67 | , m_isDataSent(false) |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | usage() |
| 73 | { |
| 74 | std::cout << "\n Usage:\n " << m_programName << " " |
| 75 | "[-f] [-D] [-i identity] [-F] [-x freshness] [-w timeout] ndn:/name\n" |
| 76 | " Reads payload from stdin and sends it to local NDN forwarder as a " |
| 77 | "single Data packet\n" |
| 78 | " [-f] - force, send Data without waiting for Interest\n" |
| 79 | " [-D] - use DigestSha256 signing method instead of " |
| 80 | "SignatureSha256WithRsa\n" |
| 81 | " [-i identity] - set identity to be used for signing\n" |
| 82 | " [-F] - set FinalBlockId to the last component of Name\n" |
| 83 | " [-x] - set FreshnessPeriod in time::milliseconds\n" |
| 84 | " [-w timeout] - set Timeout in time::milliseconds\n" |
| 85 | " [-h] - print help and exit\n" |
| 86 | " [-V] - print version and exit\n" |
| 87 | "\n"; |
| 88 | exit(1); |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | setForceData() |
| 93 | { |
| 94 | m_isForceDataSet = true; |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | setUseDigestSha256() |
| 99 | { |
| 100 | m_isUseDigestSha256Set = true; |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | setIdentityName(char* identityName) |
| 105 | { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 106 | m_identityName = make_shared<Name>(identityName); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void |
| 110 | setLastAsFinalBlockId() |
| 111 | { |
| 112 | m_isLastAsFinalBlockIdSet = true; |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | setFreshnessPeriod(int freshnessPeriod) |
| 117 | { |
| 118 | if (freshnessPeriod < 0) |
| 119 | usage(); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 120 | |
| 121 | m_freshnessPeriod = time::milliseconds(freshnessPeriod); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void |
| 125 | setTimeout(int timeout) |
| 126 | { |
| 127 | if (timeout < 0) |
| 128 | usage(); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 129 | |
| 130 | m_timeout = time::milliseconds(timeout); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void |
| 134 | setPrefixName(char* prefixName) |
| 135 | { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 136 | m_prefixName = Name(prefixName); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 139 | time::milliseconds |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 140 | getDefaultTimeout() |
| 141 | { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 142 | return time::seconds(10); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Junxiao Shi | 7348ee7 | 2015-06-07 20:57:34 -0700 | [diff] [blame] | 145 | shared_ptr<Data> |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 146 | createDataPacket() |
| 147 | { |
Junxiao Shi | 7348ee7 | 2015-06-07 20:57:34 -0700 | [diff] [blame] | 148 | auto dataPacket = make_shared<Data>(m_prefixName); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 149 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 150 | std::stringstream payloadStream; |
| 151 | payloadStream << std::cin.rdbuf(); |
| 152 | std::string payload = payloadStream.str(); |
Junxiao Shi | 7348ee7 | 2015-06-07 20:57:34 -0700 | [diff] [blame] | 153 | dataPacket->setContent(reinterpret_cast<const uint8_t*>(payload.c_str()), payload.length()); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 154 | |
| 155 | if (m_freshnessPeriod >= time::milliseconds::zero()) |
Junxiao Shi | 7348ee7 | 2015-06-07 20:57:34 -0700 | [diff] [blame] | 156 | dataPacket->setFreshnessPeriod(m_freshnessPeriod); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 157 | |
| 158 | if (m_isLastAsFinalBlockIdSet) { |
| 159 | if (!m_prefixName.empty()) |
Junxiao Shi | 7348ee7 | 2015-06-07 20:57:34 -0700 | [diff] [blame] | 160 | dataPacket->setFinalBlockId(m_prefixName.get(-1)); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 161 | else { |
| 162 | std::cerr << "Name Provided Has 0 Components" << std::endl; |
| 163 | exit(1); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 164 | } |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Eric Newberry | 38146c5 | 2015-06-22 15:11:30 -0700 | [diff] [blame^] | 167 | if (m_isUseDigestSha256Set) { |
| 168 | m_keyChain.sign(*dataPacket, signingWithSha256()); |
| 169 | } |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 170 | else { |
Eric Newberry | 38146c5 | 2015-06-22 15:11:30 -0700 | [diff] [blame^] | 171 | if (m_identityName == nullptr) { |
Junxiao Shi | 7348ee7 | 2015-06-07 20:57:34 -0700 | [diff] [blame] | 172 | m_keyChain.sign(*dataPacket); |
Eric Newberry | 38146c5 | 2015-06-22 15:11:30 -0700 | [diff] [blame^] | 173 | } |
| 174 | else { |
| 175 | m_keyChain.sign(*dataPacket, signingByIdentity(*m_identityName)); |
| 176 | } |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 179 | return dataPacket; |
| 180 | } |
| 181 | |
| 182 | void |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 183 | onInterest(const Name& name, |
| 184 | const Interest& interest, |
Junxiao Shi | 7348ee7 | 2015-06-07 20:57:34 -0700 | [diff] [blame] | 185 | shared_ptr<Data> dataPacket) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 186 | { |
Junxiao Shi | 7348ee7 | 2015-06-07 20:57:34 -0700 | [diff] [blame] | 187 | m_face.put(*dataPacket); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 188 | m_isDataSent = true; |
| 189 | m_face.shutdown(); |
| 190 | } |
| 191 | |
| 192 | void |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 193 | onRegisterFailed(const Name& prefix, const std::string& reason) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 194 | { |
| 195 | std::cerr << "Prefix Registration Failure." << std::endl; |
| 196 | std::cerr << "Reason = " << reason << std::endl; |
| 197 | } |
| 198 | |
| 199 | void |
| 200 | run() |
| 201 | { |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 202 | try { |
Junxiao Shi | 7348ee7 | 2015-06-07 20:57:34 -0700 | [diff] [blame] | 203 | shared_ptr<Data> dataPacket = createDataPacket(); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 204 | if (m_isForceDataSet) { |
Junxiao Shi | 7348ee7 | 2015-06-07 20:57:34 -0700 | [diff] [blame] | 205 | m_face.put(*dataPacket); |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 206 | m_isDataSent = true; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 207 | } |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 208 | else { |
| 209 | m_face.setInterestFilter(m_prefixName, |
| 210 | bind(&NdnPoke::onInterest, this, _1, _2, dataPacket), |
| 211 | RegisterPrefixSuccessCallback(), |
| 212 | bind(&NdnPoke::onRegisterFailed, this, _1, _2)); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 213 | } |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 214 | |
| 215 | if (m_timeout < time::milliseconds::zero()) |
| 216 | m_face.processEvents(getDefaultTimeout()); |
| 217 | else |
| 218 | m_face.processEvents(m_timeout); |
| 219 | } |
| 220 | catch (std::exception& e) { |
| 221 | std::cerr << "ERROR: " << e.what() << "\n" << std::endl; |
| 222 | exit(1); |
| 223 | } |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | bool |
| 227 | isDataSent() const |
| 228 | { |
| 229 | return m_isDataSent; |
| 230 | } |
| 231 | |
| 232 | private: |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 233 | KeyChain m_keyChain; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 234 | std::string m_programName; |
| 235 | bool m_isForceDataSet; |
| 236 | bool m_isUseDigestSha256Set; |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 237 | shared_ptr<Name> m_identityName; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 238 | bool m_isLastAsFinalBlockIdSet; |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 239 | time::milliseconds m_freshnessPeriod; |
| 240 | time::milliseconds m_timeout; |
| 241 | Name m_prefixName; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 242 | bool m_isDataSent; |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 243 | Face m_face; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 244 | }; |
| 245 | |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 246 | int |
| 247 | main(int argc, char* argv[]) |
| 248 | { |
| 249 | int option; |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 250 | NdnPoke program(argv[0]); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 251 | while ((option = getopt(argc, argv, "hfDi:Fx:w:V")) != -1) { |
| 252 | switch (option) { |
| 253 | case 'h': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 254 | program.usage(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 255 | break; |
| 256 | case 'f': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 257 | program.setForceData(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 258 | break; |
| 259 | case 'D': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 260 | program.setUseDigestSha256(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 261 | break; |
| 262 | case 'i': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 263 | program.setIdentityName(optarg); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 264 | break; |
| 265 | case 'F': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 266 | program.setLastAsFinalBlockId(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 267 | break; |
| 268 | case 'x': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 269 | program.setFreshnessPeriod(atoi(optarg)); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 270 | break; |
| 271 | case 'w': |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 272 | program.setTimeout(atoi(optarg)); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 273 | break; |
| 274 | case 'V': |
Junxiao Shi | 1753afb | 2015-04-17 20:59:50 -0700 | [diff] [blame] | 275 | std::cout << "ndnpoke " << tools::VERSION << std::endl; |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 276 | return 0; |
| 277 | default: |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 278 | program.usage(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 279 | break; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | argc -= optind; |
| 284 | argv += optind; |
| 285 | |
| 286 | if (argv[0] == 0) |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 287 | program.usage(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 288 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 289 | program.setPrefixName(argv[0]); |
| 290 | program.run(); |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 291 | |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 292 | if (program.isDataSent()) |
Junxiao Shi | 2ac79d9 | 2015-03-23 11:16:18 -0700 | [diff] [blame] | 293 | return 0; |
| 294 | else |
| 295 | return 1; |
| 296 | } |
Junxiao Shi | 0c75f99 | 2015-03-24 21:39:47 -0700 | [diff] [blame] | 297 | |
| 298 | } // namespace peek |
| 299 | } // namespace ndn |
| 300 | |
| 301 | int |
| 302 | main(int argc, char** argv) |
| 303 | { |
| 304 | return ndn::peek::main(argc, argv); |
| 305 | } |