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