blob: 31ec56188a8ae7eaa6d303fe69d6acde5b1834e8 [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/**
Alexander Afanasyev4a771362014-04-24 21:29:33 -07003 * 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
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/>.
24 *
25 * @author Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
jeraldabraham8d840002014-03-16 19:52:09 -070026 */
27
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070028#include "version.hpp"
29
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020030#include <boost/noncopyable.hpp>
Alexander Afanasyev4a771362014-04-24 21:29:33 -070031
32#include <ndn-cxx/face.hpp>
33#include <ndn-cxx/security/key-chain.hpp>
jeraldabraham8d840002014-03-16 19:52:09 -070034
35namespace ndntlvpoke {
36
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020037using ndn::_1;
38using ndn::_2;
39
jeraldabraham8d840002014-03-16 19:52:09 -070040class NdnTlvPoke : boost::noncopyable
41{
jeraldabraham8d840002014-03-16 19:52:09 -070042public:
jeraldabraham8d840002014-03-16 19:52:09 -070043 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 Afanasyeveb3197f2014-03-17 19:28:18 -070067 " [-x] - set FreshnessPeriod in time::milliseconds\n"
68 " [-w timeout] - set Timeout in time::milliseconds\n"
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070069 " [-h] - print help and exit\n"
70 " [-V] - print version and exit\n"
71 "\n";
jeraldabraham8d840002014-03-16 19:52:09 -070072 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 Afanasyeveb3197f2014-03-17 19:28:18 -0700104 m_freshnessPeriod = ndn::time::milliseconds(freshnessPeriod);
jeraldabraham8d840002014-03-16 19:52:09 -0700105 }
106
107 void
108 setTimeout(int timeout)
109 {
110 if (timeout < 0)
111 usage();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700112 m_timeout = ndn::time::milliseconds(timeout);
jeraldabraham8d840002014-03-16 19:52:09 -0700113 }
114
115 void
116 setPrefixName(char* prefixName)
117 {
118 m_prefixName = ndn::Name(prefixName);
119 }
120
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700121 ndn::time::milliseconds
jeraldabraham8d840002014-03-16 19:52:09 -0700122 getDefaultTimeout()
123 {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700124 return ndn::time::seconds(10);
jeraldabraham8d840002014-03-16 19:52:09 -0700125 }
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 Afanasyeveb3197f2014-03-17 19:28:18 -0700135 if (m_freshnessPeriod >= ndn::time::milliseconds::zero())
jeraldabraham8d840002014-03-16 19:52:09 -0700136 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 Pesaventoab1e8f22014-10-21 22:45:33 +0200190 bind(&NdnTlvPoke::onInterest, this, _1, _2, dataPacket),
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700191 ndn::RegisterPrefixSuccessCallback(),
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200192 bind(&NdnTlvPoke::onRegisterFailed, this, _1, _2));
jeraldabraham8d840002014-03-16 19:52:09 -0700193 }
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700194 if (m_timeout < ndn::time::milliseconds::zero())
jeraldabraham8d840002014-03-16 19:52:09 -0700195 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
212private:
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 Afanasyeveb3197f2014-03-17 19:28:18 -0700220 ndn::time::milliseconds m_freshnessPeriod;
221 ndn::time::milliseconds m_timeout;
jeraldabraham8d840002014-03-16 19:52:09 -0700222 ndn::Name m_prefixName;
223 bool m_isDataSent;
224 ndn::Face m_face;
225
226};
227
228}
229
230int
231main(int argc, char* argv[])
232{
233 int option;
234 ndntlvpoke::NdnTlvPoke ndnTlvPoke(argv[0]);
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700235 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;
jeraldabraham8d840002014-03-16 19:52:09 -0700264 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700265 }
jeraldabraham8d840002014-03-16 19:52:09 -0700266
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}