blob: da74c324a7f90c02415084e8c96a52517f2d0d2e [file] [log] [blame]
Alexander Afanasyev4a771362014-04-24 21:29:33 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
jeraldabraham8d840002014-03-16 19:52:09 -07002/**
Junxiao Shi5620fb22015-06-07 20:59:38 -07003 * 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.
jeraldabraham8d840002014-03-16 19:52:09 -070010 *
Alexander Afanasyev4a771362014-04-24 21:29:33 -070011 * 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/>.
jeraldabraham8d840002014-03-16 19:52:09 -070024 */
25
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070026#include "version.hpp"
27
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020028#include <boost/noncopyable.hpp>
Alexander Afanasyev4a771362014-04-24 21:29:33 -070029
30#include <ndn-cxx/face.hpp>
31#include <ndn-cxx/security/key-chain.hpp>
jeraldabraham8d840002014-03-16 19:52:09 -070032
33namespace ndntlvpoke {
34
35class NdnTlvPoke : boost::noncopyable
36{
jeraldabraham8d840002014-03-16 19:52:09 -070037public:
jeraldabraham8d840002014-03-16 19:52:09 -070038 explicit
39 NdnTlvPoke(char* programName)
40 : m_programName(programName)
41 , m_isForceDataSet(false)
42 , m_isUseDigestSha256Set(false)
43 , m_isLastAsFinalBlockIdSet(false)
44 , m_freshnessPeriod(-1)
45 , m_timeout(-1)
46 , m_isDataSent(false)
47 {
48 }
49
50 void
51 usage()
52 {
53 std::cout << "\n Usage:\n " << m_programName << " "
54 "[-f] [-D] [-i identity] [-F] [-x freshness] [-w timeout] ndn:/name\n"
55 " Reads payload from stdin and sends it to local NDN forwarder as a "
56 "single Data packet\n"
57 " [-f] - force, send Data without waiting for Interest\n"
58 " [-D] - use DigestSha256 signing method instead of "
59 "SignatureSha256WithRsa\n"
60 " [-i identity] - set identity to be used for signing\n"
61 " [-F] - set FinalBlockId to the last component of Name\n"
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070062 " [-x] - set FreshnessPeriod in time::milliseconds\n"
63 " [-w timeout] - set Timeout in time::milliseconds\n"
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070064 " [-h] - print help and exit\n"
65 " [-V] - print version and exit\n"
66 "\n";
jeraldabraham8d840002014-03-16 19:52:09 -070067 exit(1);
68 }
69
70 void
71 setForceData()
72 {
73 m_isForceDataSet = true;
74 }
75
76 void
77 setUseDigestSha256()
78 {
79 m_isUseDigestSha256Set = true;
80 }
81
82 void
83 setIdentityName(char* identityName)
84 {
85 m_identityName = ndn::make_shared<ndn::Name>(identityName);
86 }
87
88 void
89 setLastAsFinalBlockId()
90 {
91 m_isLastAsFinalBlockIdSet = true;
92 }
93
94 void
95 setFreshnessPeriod(int freshnessPeriod)
96 {
97 if (freshnessPeriod < 0)
98 usage();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070099 m_freshnessPeriod = ndn::time::milliseconds(freshnessPeriod);
jeraldabraham8d840002014-03-16 19:52:09 -0700100 }
101
102 void
103 setTimeout(int timeout)
104 {
105 if (timeout < 0)
106 usage();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700107 m_timeout = ndn::time::milliseconds(timeout);
jeraldabraham8d840002014-03-16 19:52:09 -0700108 }
109
110 void
111 setPrefixName(char* prefixName)
112 {
113 m_prefixName = ndn::Name(prefixName);
114 }
115
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700116 ndn::time::milliseconds
jeraldabraham8d840002014-03-16 19:52:09 -0700117 getDefaultTimeout()
118 {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700119 return ndn::time::seconds(10);
jeraldabraham8d840002014-03-16 19:52:09 -0700120 }
121
122 ndn::Data
123 createDataPacket()
124 {
125 ndn::Data dataPacket(m_prefixName);
126 std::stringstream payloadStream;
127 payloadStream << std::cin.rdbuf();
128 std::string payload = payloadStream.str();
129 dataPacket.setContent(reinterpret_cast<const uint8_t*>(payload.c_str()), payload.length());
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700130 if (m_freshnessPeriod >= ndn::time::milliseconds::zero())
jeraldabraham8d840002014-03-16 19:52:09 -0700131 dataPacket.setFreshnessPeriod(m_freshnessPeriod);
132 if (m_isLastAsFinalBlockIdSet)
133 {
134 if (!m_prefixName.empty())
135 dataPacket.setFinalBlockId(m_prefixName.get(-1));
136 else
137 {
138 std::cerr << "Name Provided Has 0 Components" << std::endl;
139 exit(1);
140 }
141 }
142 if (m_isUseDigestSha256Set)
143 m_keyChain.signWithSha256(dataPacket);
144 else
145 {
146 if (!static_cast<bool>(m_identityName))
147 m_keyChain.sign(dataPacket);
148 else
149 m_keyChain.signByIdentity(dataPacket, *m_identityName);
150 }
151 return dataPacket;
152 }
153
154 void
155 onInterest(const ndn::Name& name,
156 const ndn::Interest& interest,
157 const ndn::Data& dataPacket)
158 {
159 m_face.put(dataPacket);
160 m_isDataSent = true;
161 m_face.shutdown();
162 }
163
164 void
165 onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
166 {
167 std::cerr << "Prefix Registration Failure." << std::endl;
168 std::cerr << "Reason = " << reason << std::endl;
169 }
170
171 void
172 run()
173 {
174 try
175 {
176 ndn::Data dataPacket = createDataPacket();
177 if (m_isForceDataSet)
178 {
179 m_face.put(dataPacket);
180 m_isDataSent = true;
181 }
182 else
183 {
184 m_face.setInterestFilter(m_prefixName,
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200185 bind(&NdnTlvPoke::onInterest, this, _1, _2, dataPacket),
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700186 ndn::RegisterPrefixSuccessCallback(),
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200187 bind(&NdnTlvPoke::onRegisterFailed, this, _1, _2));
jeraldabraham8d840002014-03-16 19:52:09 -0700188 }
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700189 if (m_timeout < ndn::time::milliseconds::zero())
jeraldabraham8d840002014-03-16 19:52:09 -0700190 m_face.processEvents(getDefaultTimeout());
191 else
192 m_face.processEvents(m_timeout);
193 }
194 catch (std::exception& e)
195 {
196 std::cerr << "ERROR: " << e.what() << "\n" << std::endl;
197 exit(1);
198 }
199 }
200
201 bool
202 isDataSent() const
203 {
204 return m_isDataSent;
205 }
206
207private:
208
209 ndn::KeyChain m_keyChain;
210 std::string m_programName;
211 bool m_isForceDataSet;
212 bool m_isUseDigestSha256Set;
213 ndn::shared_ptr<ndn::Name> m_identityName;
214 bool m_isLastAsFinalBlockIdSet;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700215 ndn::time::milliseconds m_freshnessPeriod;
216 ndn::time::milliseconds m_timeout;
jeraldabraham8d840002014-03-16 19:52:09 -0700217 ndn::Name m_prefixName;
218 bool m_isDataSent;
219 ndn::Face m_face;
220
221};
222
223}
224
225int
226main(int argc, char* argv[])
227{
Junxiao Shi5620fb22015-06-07 20:59:38 -0700228 std::cerr << "ndn-tlv-poke is deprecated. Use ndnpoke program from ndn-tools repository.\n"
229 "See `man ndn-tlv-poke` for details." << std::endl;
230
jeraldabraham8d840002014-03-16 19:52:09 -0700231 int option;
232 ndntlvpoke::NdnTlvPoke ndnTlvPoke(argv[0]);
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700233 while ((option = getopt(argc, argv, "hfDi:Fx:w:V")) != -1) {
234 switch (option) {
235 case 'h':
236 ndnTlvPoke.usage();
237 break;
238 case 'f':
239 ndnTlvPoke.setForceData();
240 break;
241 case 'D':
242 ndnTlvPoke.setUseDigestSha256();
243 break;
244 case 'i':
245 ndnTlvPoke.setIdentityName(optarg);
246 break;
247 case 'F':
248 ndnTlvPoke.setLastAsFinalBlockId();
249 break;
250 case 'x':
251 ndnTlvPoke.setFreshnessPeriod(atoi(optarg));
252 break;
253 case 'w':
254 ndnTlvPoke.setTimeout(atoi(optarg));
255 break;
256 case 'V':
257 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
258 return 0;
259 default:
260 ndnTlvPoke.usage();
261 break;
jeraldabraham8d840002014-03-16 19:52:09 -0700262 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700263 }
jeraldabraham8d840002014-03-16 19:52:09 -0700264
265 argc -= optind;
266 argv += optind;
267
268 if (argv[0] == 0)
269 ndnTlvPoke.usage();
270
271 ndnTlvPoke.setPrefixName(argv[0]);
272 ndnTlvPoke.run();
273
274 if (ndnTlvPoke.isDataSent())
275 return 0;
276 else
277 return 1;
278}