blob: 91148de43723499384782c1d7338a2f655e3288a [file] [log] [blame]
Alexander Afanasyevf278db32013-01-21 14:41:01 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080022#include "ccnx-wrapper.h"
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080023extern "C" {
24#include <ccn/fetch.h>
25}
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080026#include <poll.h>
27#include <boost/throw_exception.hpp>
28#include <boost/date_time/posix_time/posix_time.hpp>
29#include <boost/random.hpp>
Alexander Afanasyevf278db32013-01-21 14:41:01 -080030#include <boost/make_shared.hpp>
Zhenkai Zhucbbb8882013-01-25 13:49:12 -080031#include <boost/algorithm/string.hpp>
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080032#include <sstream>
33
Alexander Afanasyev9c9c3012013-01-25 23:44:25 -080034#include "logging.h"
35
36INIT_LOGGER ("Ccnx.Wrapper");
37
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080038typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
39typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int;
40
41using namespace std;
42using namespace boost;
43
44namespace Ccnx {
45
Zhenkai Zhu5acebd72013-02-07 19:59:25 -080046static int
47ccn_encode_Signature(struct ccn_charbuf *buf,
48 const char *digest_algorithm,
49 const void *witness,
50 size_t witness_size,
51 const struct ccn_signature *signature,
52 size_t signature_size)
53{
54 int res = 0;
55
56 if (signature == NULL)
57 return(-1);
58
59 res |= ccn_charbuf_append_tt(buf, CCN_DTAG_Signature, CCN_DTAG);
60
61 if (digest_algorithm != NULL) {
62 res |= ccn_charbuf_append_tt(buf, CCN_DTAG_DigestAlgorithm, CCN_DTAG);
63 res |= ccn_charbuf_append_tt(buf, strlen(digest_algorithm), CCN_UDATA);
64 res |= ccn_charbuf_append_string(buf, digest_algorithm);
65 res |= ccn_charbuf_append_closer(buf);
66 }
67
68 if (witness != NULL) {
69 res |= ccn_charbuf_append_tt(buf, CCN_DTAG_Witness, CCN_DTAG);
70 res |= ccn_charbuf_append_tt(buf, witness_size, CCN_BLOB);
71 res |= ccn_charbuf_append(buf, witness, witness_size);
72 res |= ccn_charbuf_append_closer(buf);
73 }
74
75 res |= ccn_charbuf_append_tt(buf, CCN_DTAG_SignatureBits, CCN_DTAG);
76 res |= ccn_charbuf_append_tt(buf, signature_size, CCN_BLOB);
77 res |= ccn_charbuf_append(buf, signature, signature_size);
78 res |= ccn_charbuf_append_closer(buf);
79
80 res |= ccn_charbuf_append_closer(buf);
81
82 return(res == 0 ? 0 : -1);
83}
84
85static int
86ccn_pack_ContentObject(struct ccn_charbuf *buf,
87 const struct ccn_charbuf *Name,
88 const struct ccn_charbuf *SignedInfo,
89 const void *data,
90 size_t size,
91 const char *digest_algorithm,
92 const struct ccn_pkey *private_key
93 )
94{
95 int res = 0;
96 struct ccn_sigc *sig_ctx;
97 struct ccn_signature *signature;
98 struct ccn_charbuf *content_header;
99 size_t closer_start;
100
101 content_header = ccn_charbuf_create();
102 res |= ccn_charbuf_append_tt(content_header, CCN_DTAG_Content, CCN_DTAG);
103 if (size != 0)
104 res |= ccn_charbuf_append_tt(content_header, size, CCN_BLOB);
105 closer_start = content_header->length;
106 res |= ccn_charbuf_append_closer(content_header);
107 if (res < 0)
108 return(-1);
109 sig_ctx = ccn_sigc_create();
110 size_t sig_size = ccn_sigc_signature_max_size(sig_ctx, private_key);
111 signature = (ccn_signature *)calloc(1, sig_size);
112 ccn_sigc_destroy(&sig_ctx);
113 res |= ccn_charbuf_append_tt(buf, CCN_DTAG_ContentObject, CCN_DTAG);
114
115 res |= ccn_encode_Signature(buf, digest_algorithm,
116 NULL, 0, signature, sig_size);
117 res |= ccn_charbuf_append_charbuf(buf, Name);
118 res |= ccn_charbuf_append_charbuf(buf, SignedInfo);
119 res |= ccnb_append_tagged_blob(buf, CCN_DTAG_Content, data, size);
120 res |= ccn_charbuf_append_closer(buf);
121 free(signature);
122 ccn_charbuf_destroy(&content_header);
123 return(res == 0 ? 0 : -1);
124}
125
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800126CcnxWrapper::CcnxWrapper()
127 : m_handle (0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800128 , m_running (true)
129 , m_connected (false)
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800130 , m_executor (new Executor(1))
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800131 , m_keystore(NULL)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800132{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800133 start ();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800134}
135
136void
137CcnxWrapper::connectCcnd()
138{
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800139 //if (m_handle != 0) {
140 //ccn_disconnect (m_handle);
141 //ccn_destroy (&m_handle);
142 //}
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800143
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800144 m_handle = ccn_create ();
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800145 //UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800146 if (ccn_connect(m_handle, NULL) < 0)
147 {
148 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
149 }
150 m_connected = true;
151
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800152 //if (!m_registeredInterests.empty())
153 //{
154 // for (map<Name, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it)
155 //{
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800156 // clearInterestFilter(it->first);
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800157 // setInterestFilter(it->first, it->second);
158 //}
159 //}
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800160}
161
162CcnxWrapper::~CcnxWrapper()
163{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800164 shutdown ();
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800165 if (m_keystore != NULL)
166 {
167 ccn_keystore_destroy(&m_keystore);
168 }
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800169}
170
171void
172CcnxWrapper::start () // called automatically in constructor
173{
174 connectCcnd();
175 m_thread = thread (&CcnxWrapper::ccnLoop, this);
176 m_executor->start();
177}
178
179void
180CcnxWrapper::shutdown () // called in destructor, but can called manually
181{
182 m_executor->shutdown();
183
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800184 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800185 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800186 m_running = false;
187 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800188
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800189 _LOG_DEBUG ("+++++++++SHUTDOWN+++++++");
190 if (m_connected)
191 {
192 m_thread.join ();
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800193
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800194 ccn_disconnect (m_handle);
195 //ccn_destroy (&m_handle);
196 m_connected = false;
197 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800198}
199
200void
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800201CcnxWrapper::ccnLoop ()
202{
203 static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0)));
204 static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000));
205
206 while (m_running)
207 {
208 try
209 {
210 int res = 0;
211 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800212 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800213 res = ccn_run (m_handle, 0);
214 }
215
216 if (!m_running) break;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800217
Alexander Afanasyev9c9c3012013-01-25 23:44:25 -0800218 if (res < 0) {
219 _LOG_ERROR ("ccn_run returned negative status: " << res);
220 usleep (100000);
221 continue;
222 // BOOST_THROW_EXCEPTION (CcnxOperationException()
223 // << errmsg_info_str("ccn_run returned error"));
224 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800225
226
227 pollfd pfds[1];
228 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800229 UniqueRecLock lock(m_mutex);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800230
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800231 pfds[0].fd = ccn_get_connection_fd (m_handle);
232 pfds[0].events = POLLIN;
233 if (ccn_output_is_pending (m_handle))
234 pfds[0].events |= POLLOUT;
235 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800236
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800237 int ret = poll (pfds, 1, 1);
238 if (ret < 0)
239 {
240 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
241 }
242 }
243 catch (CcnxOperationException &e)
244 {
245 // do not try reconnect for now
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800246 cout << *get_error_info<errmsg_info_str> (e) << endl;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800247 throw e;
248 /*
249 m_connected = false;
250 // probably ccnd has been stopped
251 // try reconnect with sleep
252 int interval = 1;
253 int maxInterval = 32;
254 while (m_running)
255 {
256 try
257 {
258 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
259
260 connectCcnd();
261 _LOG_DEBUG("reconnect to ccnd succeeded");
262 break;
263 }
264 catch (CcnxOperationException &e)
265 {
266 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
267
268 // do exponential backup for reconnect interval
269 if (interval < maxInterval)
270 {
271 interval *= 2;
272 }
273 }
274 }
275 */
276 }
277 catch (const std::exception &exc)
278 {
279 // catch anything thrown within try block that derives from std::exception
280 std::cerr << exc.what();
281 }
282 catch (...)
283 {
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800284 cout << "UNKNOWN EXCEPTION !!!" << endl;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800285 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800286 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800287}
288
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800289Bytes
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800290CcnxWrapper::createContentObject(const Name &name, const void *buf, size_t len, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800291{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800292 {
293 UniqueRecLock lock(m_mutex);
294 if (!m_running || !m_connected)
295 {
296 _LOG_TRACE ("<< not running or connected");
297 return Bytes ();
298 }
299 }
300
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800301 CcnxCharbufPtr ptr = name.toCcnxCharbuf();
302 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800303 ccn_charbuf *content = ccn_charbuf_create();
304
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800305 struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
306 sp.freshness = freshness;
307
308 if (ccn_sign_content(m_handle, content, pname, &sp, buf, len) != 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800309 {
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800310 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("sign content failed"));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800311 }
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800312
313 Bytes bytes;
314 readRaw(bytes, content->buf, content->length);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800315
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800316 ccn_charbuf_destroy (&content);
317
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800318 return bytes;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800319}
320
321int
Zhenkai Zhubad089c2012-12-28 10:28:27 -0800322CcnxWrapper::putToCcnd (const Bytes &contentObject)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800323{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800324 _LOG_TRACE (">> putToCcnd");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800325 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800326 if (!m_running || !m_connected)
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800327 {
328 _LOG_TRACE ("<< not running or connected");
329 return -1;
330 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800331
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800332
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800333 if (ccn_put(m_handle, head(contentObject), contentObject.size()) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800334 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800335 _LOG_ERROR ("ccn_put failed");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800336 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
337 }
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800338 else
339 {
340 _LOG_DEBUG ("<< putToCcnd");
341 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800342
343 return 0;
344}
345
346int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800347CcnxWrapper::publishData (const Name &name, const unsigned char *buf, size_t len, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800348{
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800349 Bytes co = createContentObject(name, buf, len, freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800350 return putToCcnd(co);
351}
352
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800353int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800354CcnxWrapper::publishData (const Name &name, const Bytes &content, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800355{
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800356 return publishData(name, head(content), content.size(), freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800357}
358
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800359int
360CcnxWrapper::publishUnsignedData(const Name &name, const Bytes &content, int freshness)
361{
362 return publishUnsignedData(name, head(content), content.size(), freshness);
363}
364
365int
366CcnxWrapper::publishUnsignedData(const Name &name, const unsigned char *buf, size_t len, int freshness)
367{
368 {
369 UniqueRecLock lock(m_mutex);
370 if (!m_running || !m_connected)
371 {
372 _LOG_TRACE ("<< not running or connected");
373 return -1;
374 }
375 }
376
377 CcnxCharbufPtr ptr = name.toCcnxCharbuf();
378 ccn_charbuf *pname = ptr->getBuf();
379 ccn_charbuf *content = ccn_charbuf_create();
380 ccn_charbuf *signed_info = ccn_charbuf_create();
381
382 if (m_keystore == NULL)
383 {
384 m_keystore = ccn_keystore_create ();
385 string keystoreFile = string(getenv("HOME")) + string("/.ccnx/.ccnx_keystore");
386 if (ccn_keystore_init (m_keystore, (char *)keystoreFile.c_str(), (char*)"Th1s1sn0t8g00dp8ssw0rd.") < 0)
387 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str(keystoreFile.c_str()));
388 }
389
390 int res = ccn_signed_info_create(signed_info,
391 ccn_keystore_public_key_digest(m_keystore),
392 ccn_keystore_public_key_digest_length(m_keystore),
393 NULL,
394 CCN_CONTENT_DATA,
395 freshness,
396 NULL,
397 NULL
398 );
399
400 ccn_pack_ContentObject(content, pname, signed_info, buf, len, NULL, ccn_keystore_private_key (m_keystore));
401
402 Bytes bytes;
403 readRaw(bytes, content->buf, content->length);
404
405 ccn_charbuf_destroy (&content);
406 ccn_charbuf_destroy (&signed_info);
407
408 return putToCcnd (bytes);
409}
410
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800411
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800412static void
413deleterInInterestTuple (tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> *tuple)
414{
415 delete tuple->get<0> ();
416 delete tuple;
417}
418
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800419static ccn_upcall_res
420incomingInterest(ccn_closure *selfp,
421 ccn_upcall_kind kind,
422 ccn_upcall_info *info)
423{
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800424 CcnxWrapper::InterestCallback *f;
425 ExecutorPtr executor;
426 tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> *realData = reinterpret_cast< tuple<CcnxWrapper::InterestCallback *, ExecutorPtr>* > (selfp->data);
427 tie (f, executor) = *realData;
428
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800429 switch (kind)
430 {
431 case CCN_UPCALL_FINAL: // effective in unit tests
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800432 // delete closure;
433 executor->execute (bind (deleterInInterestTuple, realData));
434
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800435 delete selfp;
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800436 _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_FINAL");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800437 return CCN_UPCALL_RESULT_OK;
438
439 case CCN_UPCALL_INTEREST:
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800440 _LOG_TRACE (">> incomingInterest upcall: " << Name(info->interest_ccnb, info->interest_comps));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800441 break;
442
443 default:
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800444 _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_RESULT_OK: " << Name(info->interest_ccnb, info->interest_comps));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800445 return CCN_UPCALL_RESULT_OK;
446 }
447
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800448 Name interest(info->interest_ccnb, info->interest_comps);
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800449 Selectors selectors(info->pi);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800450
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800451 executor->execute (bind (*f, interest, selectors));
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800452 // this will be run in executor
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800453 // (*f) (interest);
454 // closure->runInterestCallback(interest);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800455
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800456 return CCN_UPCALL_RESULT_OK;
457}
458
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800459static void
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800460deleterInDataTuple (tuple<Closure *, ExecutorPtr, Selectors> *tuple)
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800461{
462 delete tuple->get<0> ();
463 delete tuple;
464}
465
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800466static ccn_upcall_res
467incomingData(ccn_closure *selfp,
468 ccn_upcall_kind kind,
469 ccn_upcall_info *info)
470{
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800471 // Closure *cp = static_cast<Closure *> (selfp->data);
472 Closure *cp;
473 ExecutorPtr executor;
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800474 Selectors selectors;
475 tuple<Closure *, ExecutorPtr, Selectors> *realData = reinterpret_cast< tuple<Closure*, ExecutorPtr, Selectors>* > (selfp->data);
476 tie (cp, executor, selectors) = *realData;
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800477
Zhenkai Zhu79264a42013-02-07 21:49:42 -0800478 bool verified = false;
479
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800480 switch (kind)
481 {
482 case CCN_UPCALL_FINAL: // effecitve in unit tests
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800483 executor->execute (bind (deleterInDataTuple, realData));
484
Zhenkai Zhu19f81de2013-01-04 22:27:47 -0800485 cp = NULL;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800486 delete selfp;
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800487 _LOG_TRACE ("<< incomingData with CCN_UPCALL_FINAL");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800488 return CCN_UPCALL_RESULT_OK;
489
490 case CCN_UPCALL_CONTENT:
Zhenkai Zhu79264a42013-02-07 21:49:42 -0800491 verified = true;
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800492 _LOG_TRACE (">> incomingData content upcall: " << Name (info->content_ccnb, info->content_comps));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800493 break;
494
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800495 case CCN_UPCALL_CONTENT_UNVERIFIED:
496 _LOG_TRACE (">> incomingData content unverified upcall: " << Name (info->content_ccnb, info->content_comps));
497 break;
498
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800499 case CCN_UPCALL_INTEREST_TIMED_OUT: {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800500 if (cp != NULL)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800501 {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800502 Name interest(info->interest_ccnb, info->interest_comps);
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800503 _LOG_TRACE ("<< incomingData timeout: " << Name (info->interest_ccnb, info->interest_comps));
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800504 executor->execute (bind (&Closure::runTimeoutCallback, cp, interest, *cp, selectors));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800505 }
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800506 else
507 {
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800508 _LOG_TRACE ("<< incomingData timeout, but callback is not set...: " << Name (info->interest_ccnb, info->interest_comps));
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800509 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800510 return CCN_UPCALL_RESULT_OK;
511 }
512
513 default:
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800514 _LOG_TRACE(">> unknown upcall type");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800515 return CCN_UPCALL_RESULT_OK;
516 }
517
Zhenkai Zhu79264a42013-02-07 21:49:42 -0800518 PcoPtr pco = make_shared<ParsedContentObject> (info->content_ccnb, info->pco->offset[CCN_PCO_E], verified);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800519
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800520 // this will be run in executor
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800521 executor->execute (bind (&Closure::runDataCallback, cp, pco->name (), pco));
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800522 _LOG_TRACE (">> incomingData");
523
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800524 return CCN_UPCALL_RESULT_OK;
525}
526
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800527int CcnxWrapper::sendInterest (const Name &interest, const Closure &closure, const Selectors &selectors)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800528{
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800529 _LOG_TRACE (">> sendInterest: " << interest);
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800530 {
531 UniqueRecLock lock(m_mutex);
532 if (!m_running || !m_connected)
Alexander Afanasyevb3d4cd52013-01-28 11:20:52 -0800533 {
534 _LOG_ERROR ("<< sendInterest: not running or connected");
535 return -1;
536 }
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800537 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800538
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800539 CcnxCharbufPtr namePtr = interest.toCcnxCharbuf();
540 ccn_charbuf *pname = namePtr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800541 ccn_closure *dataClosure = new ccn_closure;
542
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800543 // Closure *myClosure = new ExecutorClosure(closure, m_executor);
544 Closure *myClosure = closure.dup ();
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800545 dataClosure->data = new tuple<Closure*, ExecutorPtr, Selectors> (myClosure, m_executor, selectors);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800546
547 dataClosure->p = &incomingData;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800548
549 CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf();
550 ccn_charbuf *templ = NULL;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800551 if (selectorsPtr)
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800552 {
553 templ = selectorsPtr->getBuf();
554 }
555
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800556 UniqueRecLock lock(m_mutex);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800557 if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800558 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800559 _LOG_ERROR ("<< sendInterest: ccn_express_interest FAILED!!!");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800560 }
561
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800562 return 0;
563}
564
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800565int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800566{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800567 _LOG_TRACE (">> setInterestFilter");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800568 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800569 if (!m_running || !m_connected)
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800570 {
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800571 return -1;
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800572 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800573
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800574 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
575 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800576 ccn_closure *interestClosure = new ccn_closure;
577
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800578 // interestClosure->data = new ExecutorInterestClosure(interestCallback, m_executor);
579
580 interestClosure->data = new tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> (new InterestCallback (interestCallback), m_executor); // should be removed when closure is removed
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800581 interestClosure->p = &incomingInterest;
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800582
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800583 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
584 if (ret < 0)
585 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800586 _LOG_ERROR ("<< setInterestFilter: ccn_set_interest_filter FAILED");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800587 }
588
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800589 m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback));
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800590
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800591 _LOG_TRACE ("<< setInterestFilter");
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800592
593 return ret;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800594}
595
596void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800597CcnxWrapper::clearInterestFilter (const Name &prefix)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800598{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800599 _LOG_TRACE (">> clearInterestFilter");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800600 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800601 if (!m_running || !m_connected)
602 return;
603
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800604 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
605 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800606
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800607 int ret = ccn_set_interest_filter (m_handle, pname, 0);
608 if (ret < 0)
609 {
610 }
611
612 m_registeredInterests.erase(prefix);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800613
614 _LOG_TRACE ("<< clearInterestFilter");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800615}
616
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800617Name
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800618CcnxWrapper::getLocalPrefix ()
619{
620 struct ccn * tmp_handle = ccn_create ();
621 int res = ccn_connect (tmp_handle, NULL);
622 if (res < 0)
623 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800624 return Name();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800625 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800626
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800627 string retval = "";
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800628
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800629 struct ccn_charbuf *templ = ccn_charbuf_create();
630 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
631 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
632 ccn_charbuf_append_closer(templ); /* </Name> */
633 // XXX - use pubid if possible
634 ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG);
635 ccnb_append_number(templ, 1);
636 ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */
637 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2);
638 ccn_charbuf_append_closer(templ); /* </Interest> */
639
640 struct ccn_charbuf *name = ccn_charbuf_create ();
641 res = ccn_name_from_uri (name, "/local/ndn/prefix");
642 if (res < 0) {
643 }
644 else
645 {
646 struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800647
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800648 struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix",
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800649 NULL, 4, CCN_V_HIGHEST, 0);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800650 if (stream == NULL) {
651 }
652 else
653 {
654 ostringstream os;
655
656 int counter = 0;
657 char buf[256];
658 while (true) {
659 res = ccn_fetch_read (stream, buf, sizeof(buf));
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800660
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800661 if (res == 0) {
662 break;
663 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800664
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800665 if (res > 0) {
666 os << string(buf, res);
667 } else if (res == CCN_FETCH_READ_NONE) {
668 if (counter < 2)
669 {
670 ccn_run(tmp_handle, 1000);
671 counter ++;
672 }
673 else
674 {
675 break;
676 }
677 } else if (res == CCN_FETCH_READ_END) {
678 break;
679 } else if (res == CCN_FETCH_READ_TIMEOUT) {
680 break;
681 } else {
682 break;
683 }
684 }
685 retval = os.str ();
686 stream = ccn_fetch_close(stream);
687 }
688 fetch = ccn_fetch_destroy(fetch);
689 }
690
691 ccn_charbuf_destroy (&name);
692
693 ccn_disconnect (tmp_handle);
694 ccn_destroy (&tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800695
Zhenkai Zhucbbb8882013-01-25 13:49:12 -0800696 boost::algorithm::trim(retval);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800697 return Name(retval);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800698}
699
Zhenkai Zhu79264a42013-02-07 21:49:42 -0800700bool
701CcnxWrapper::verifyPco(PcoPtr &pco)
702{
703 bool verified = ccn_verify_content(m_handle, pco->msg(), (ccn_parsed_ContentObject *)pco->pco());
704 pco->setVerified(verified);
705 return verified;
706}
707
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800708}