blob: 3f5a3b61c50c89f7f70f776105adbf74e0b29204 [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
jeraldabraham8d840002014-03-16 19:52:09 -070030#include <boost/utility.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
37class NdnTlvPoke : boost::noncopyable
38{
39
40public:
41
42 explicit
43 NdnTlvPoke(char* programName)
44 : m_programName(programName)
45 , m_isForceDataSet(false)
46 , m_isUseDigestSha256Set(false)
47 , m_isLastAsFinalBlockIdSet(false)
48 , m_freshnessPeriod(-1)
49 , m_timeout(-1)
50 , m_isDataSent(false)
51 {
52 }
53
54 void
55 usage()
56 {
57 std::cout << "\n Usage:\n " << m_programName << " "
58 "[-f] [-D] [-i identity] [-F] [-x freshness] [-w timeout] ndn:/name\n"
59 " Reads payload from stdin and sends it to local NDN forwarder as a "
60 "single Data packet\n"
61 " [-f] - force, send Data without waiting for Interest\n"
62 " [-D] - use DigestSha256 signing method instead of "
63 "SignatureSha256WithRsa\n"
64 " [-i identity] - set identity to be used for signing\n"
65 " [-F] - set FinalBlockId to the last component of Name\n"
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070066 " [-x] - set FreshnessPeriod in time::milliseconds\n"
67 " [-w timeout] - set Timeout in time::milliseconds\n"
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070068 " [-h] - print help and exit\n"
69 " [-V] - print version and exit\n"
70 "\n";
jeraldabraham8d840002014-03-16 19:52:09 -070071 exit(1);
72 }
73
74 void
75 setForceData()
76 {
77 m_isForceDataSet = true;
78 }
79
80 void
81 setUseDigestSha256()
82 {
83 m_isUseDigestSha256Set = true;
84 }
85
86 void
87 setIdentityName(char* identityName)
88 {
89 m_identityName = ndn::make_shared<ndn::Name>(identityName);
90 }
91
92 void
93 setLastAsFinalBlockId()
94 {
95 m_isLastAsFinalBlockIdSet = true;
96 }
97
98 void
99 setFreshnessPeriod(int freshnessPeriod)
100 {
101 if (freshnessPeriod < 0)
102 usage();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700103 m_freshnessPeriod = ndn::time::milliseconds(freshnessPeriod);
jeraldabraham8d840002014-03-16 19:52:09 -0700104 }
105
106 void
107 setTimeout(int timeout)
108 {
109 if (timeout < 0)
110 usage();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700111 m_timeout = ndn::time::milliseconds(timeout);
jeraldabraham8d840002014-03-16 19:52:09 -0700112 }
113
114 void
115 setPrefixName(char* prefixName)
116 {
117 m_prefixName = ndn::Name(prefixName);
118 }
119
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700120 ndn::time::milliseconds
jeraldabraham8d840002014-03-16 19:52:09 -0700121 getDefaultTimeout()
122 {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700123 return ndn::time::seconds(10);
jeraldabraham8d840002014-03-16 19:52:09 -0700124 }
125
126 ndn::Data
127 createDataPacket()
128 {
129 ndn::Data dataPacket(m_prefixName);
130 std::stringstream payloadStream;
131 payloadStream << std::cin.rdbuf();
132 std::string payload = payloadStream.str();
133 dataPacket.setContent(reinterpret_cast<const uint8_t*>(payload.c_str()), payload.length());
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700134 if (m_freshnessPeriod >= ndn::time::milliseconds::zero())
jeraldabraham8d840002014-03-16 19:52:09 -0700135 dataPacket.setFreshnessPeriod(m_freshnessPeriod);
136 if (m_isLastAsFinalBlockIdSet)
137 {
138 if (!m_prefixName.empty())
139 dataPacket.setFinalBlockId(m_prefixName.get(-1));
140 else
141 {
142 std::cerr << "Name Provided Has 0 Components" << std::endl;
143 exit(1);
144 }
145 }
146 if (m_isUseDigestSha256Set)
147 m_keyChain.signWithSha256(dataPacket);
148 else
149 {
150 if (!static_cast<bool>(m_identityName))
151 m_keyChain.sign(dataPacket);
152 else
153 m_keyChain.signByIdentity(dataPacket, *m_identityName);
154 }
155 return dataPacket;
156 }
157
158 void
159 onInterest(const ndn::Name& name,
160 const ndn::Interest& interest,
161 const ndn::Data& dataPacket)
162 {
163 m_face.put(dataPacket);
164 m_isDataSent = true;
165 m_face.shutdown();
166 }
167
168 void
169 onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
170 {
171 std::cerr << "Prefix Registration Failure." << std::endl;
172 std::cerr << "Reason = " << reason << std::endl;
173 }
174
175 void
176 run()
177 {
178 try
179 {
180 ndn::Data dataPacket = createDataPacket();
181 if (m_isForceDataSet)
182 {
183 m_face.put(dataPacket);
184 m_isDataSent = true;
185 }
186 else
187 {
188 m_face.setInterestFilter(m_prefixName,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700189 ndn::bind(&NdnTlvPoke::onInterest,
190 this, _1, _2, dataPacket),
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700191 ndn::RegisterPrefixSuccessCallback(),
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700192 ndn::bind(&NdnTlvPoke::onRegisterFailed,
193 this, _1, _2));
jeraldabraham8d840002014-03-16 19:52:09 -0700194 }
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700195 if (m_timeout < ndn::time::milliseconds::zero())
jeraldabraham8d840002014-03-16 19:52:09 -0700196 m_face.processEvents(getDefaultTimeout());
197 else
198 m_face.processEvents(m_timeout);
199 }
200 catch (std::exception& e)
201 {
202 std::cerr << "ERROR: " << e.what() << "\n" << std::endl;
203 exit(1);
204 }
205 }
206
207 bool
208 isDataSent() const
209 {
210 return m_isDataSent;
211 }
212
213private:
214
215 ndn::KeyChain m_keyChain;
216 std::string m_programName;
217 bool m_isForceDataSet;
218 bool m_isUseDigestSha256Set;
219 ndn::shared_ptr<ndn::Name> m_identityName;
220 bool m_isLastAsFinalBlockIdSet;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700221 ndn::time::milliseconds m_freshnessPeriod;
222 ndn::time::milliseconds m_timeout;
jeraldabraham8d840002014-03-16 19:52:09 -0700223 ndn::Name m_prefixName;
224 bool m_isDataSent;
225 ndn::Face m_face;
226
227};
228
229}
230
231int
232main(int argc, char* argv[])
233{
234 int option;
235 ndntlvpoke::NdnTlvPoke ndnTlvPoke(argv[0]);
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700236 while ((option = getopt(argc, argv, "hfDi:Fx:w:V")) != -1) {
237 switch (option) {
238 case 'h':
239 ndnTlvPoke.usage();
240 break;
241 case 'f':
242 ndnTlvPoke.setForceData();
243 break;
244 case 'D':
245 ndnTlvPoke.setUseDigestSha256();
246 break;
247 case 'i':
248 ndnTlvPoke.setIdentityName(optarg);
249 break;
250 case 'F':
251 ndnTlvPoke.setLastAsFinalBlockId();
252 break;
253 case 'x':
254 ndnTlvPoke.setFreshnessPeriod(atoi(optarg));
255 break;
256 case 'w':
257 ndnTlvPoke.setTimeout(atoi(optarg));
258 break;
259 case 'V':
260 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
261 return 0;
262 default:
263 ndnTlvPoke.usage();
264 break;
jeraldabraham8d840002014-03-16 19:52:09 -0700265 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700266 }
jeraldabraham8d840002014-03-16 19:52:09 -0700267
268 argc -= optind;
269 argv += optind;
270
271 if (argv[0] == 0)
272 ndnTlvPoke.usage();
273
274 ndnTlvPoke.setPrefixName(argv[0]);
275 ndnTlvPoke.run();
276
277 if (ndnTlvPoke.isDataSent())
278 return 0;
279 else
280 return 1;
281}