blob: 5ff5a50ec85bb8a4062b448b4bda7c0f267dba54 [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
28#include <boost/utility.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{
37
38public:
39
40 explicit
41 NdnTlvPoke(char* programName)
42 : m_programName(programName)
43 , m_isForceDataSet(false)
44 , m_isUseDigestSha256Set(false)
45 , m_isLastAsFinalBlockIdSet(false)
46 , m_freshnessPeriod(-1)
47 , m_timeout(-1)
48 , m_isDataSent(false)
49 {
50 }
51
52 void
53 usage()
54 {
55 std::cout << "\n Usage:\n " << m_programName << " "
56 "[-f] [-D] [-i identity] [-F] [-x freshness] [-w timeout] ndn:/name\n"
57 " Reads payload from stdin and sends it to local NDN forwarder as a "
58 "single Data packet\n"
59 " [-f] - force, send Data without waiting for Interest\n"
60 " [-D] - use DigestSha256 signing method instead of "
61 "SignatureSha256WithRsa\n"
62 " [-i identity] - set identity to be used for signing\n"
63 " [-F] - set FinalBlockId to the last component of Name\n"
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070064 " [-x] - set FreshnessPeriod in time::milliseconds\n"
65 " [-w timeout] - set Timeout in time::milliseconds\n"
jeraldabraham8d840002014-03-16 19:52:09 -070066 " [-h] - print help and exit\n\n";
67 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,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700185 ndn::bind(&NdnTlvPoke::onInterest,
186 this, _1, _2, dataPacket),
187 ndn::bind(&NdnTlvPoke::onRegisterFailed,
188 this, _1, _2));
jeraldabraham8d840002014-03-16 19:52:09 -0700189 }
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700190 if (m_timeout < ndn::time::milliseconds::zero())
jeraldabraham8d840002014-03-16 19:52:09 -0700191 m_face.processEvents(getDefaultTimeout());
192 else
193 m_face.processEvents(m_timeout);
194 }
195 catch (std::exception& e)
196 {
197 std::cerr << "ERROR: " << e.what() << "\n" << std::endl;
198 exit(1);
199 }
200 }
201
202 bool
203 isDataSent() const
204 {
205 return m_isDataSent;
206 }
207
208private:
209
210 ndn::KeyChain m_keyChain;
211 std::string m_programName;
212 bool m_isForceDataSet;
213 bool m_isUseDigestSha256Set;
214 ndn::shared_ptr<ndn::Name> m_identityName;
215 bool m_isLastAsFinalBlockIdSet;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700216 ndn::time::milliseconds m_freshnessPeriod;
217 ndn::time::milliseconds m_timeout;
jeraldabraham8d840002014-03-16 19:52:09 -0700218 ndn::Name m_prefixName;
219 bool m_isDataSent;
220 ndn::Face m_face;
221
222};
223
224}
225
226int
227main(int argc, char* argv[])
228{
229 int option;
230 ndntlvpoke::NdnTlvPoke ndnTlvPoke(argv[0]);
231 while ((option = getopt(argc, argv, "hfDi:Fx:w:")) != -1)
232 {
233 switch (option) {
234 case 'h':
235 ndnTlvPoke.usage();
236 break;
237 case 'f':
238 ndnTlvPoke.setForceData();
239 break;
240 case 'D':
241 ndnTlvPoke.setUseDigestSha256();
242 break;
243 case 'i':
244 ndnTlvPoke.setIdentityName(optarg);
245 break;
246 case 'F':
247 ndnTlvPoke.setLastAsFinalBlockId();
248 break;
249 case 'x':
250 ndnTlvPoke.setFreshnessPeriod(atoi(optarg));
251 break;
252 case 'w':
253 ndnTlvPoke.setTimeout(atoi(optarg));
254 break;
255 default:
256 ndnTlvPoke.usage();
257 break;
258 }
259 }
260
261 argc -= optind;
262 argv += optind;
263
264 if (argv[0] == 0)
265 ndnTlvPoke.usage();
266
267 ndnTlvPoke.setPrefixName(argv[0]);
268 ndnTlvPoke.run();
269
270 if (ndnTlvPoke.isDataSent())
271 return 0;
272 else
273 return 1;
274}