blob: 645f5fc78c3d9a67961f0f66ca023fb454e6098a [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
Alexander Afanasyev848a8e42013-02-07 21:19:18 -080046// hack to enable fake signatures
47// min length for signature field is 16, as defined in ccn_buf_decoder.c:728
48const int DEFAULT_SIGNATURE_SIZE = 16;
49
50// Although ccn_buf_decoder.c:745 defines minimum length 16, something else is checking and only 32-byte fake value is accepted by ccnd
51const int PUBLISHER_KEY_SIZE = 32;
52
Zhenkai Zhu5acebd72013-02-07 19:59:25 -080053static int
Alexander Afanasyev848a8e42013-02-07 21:19:18 -080054ccn_encode_garbage_Signature(struct ccn_charbuf *buf)
Zhenkai Zhu5acebd72013-02-07 19:59:25 -080055{
56 int res = 0;
57
Zhenkai Zhu5acebd72013-02-07 19:59:25 -080058 res |= ccn_charbuf_append_tt(buf, CCN_DTAG_Signature, CCN_DTAG);
59
Alexander Afanasyev848a8e42013-02-07 21:19:18 -080060 // Let's cheat more. Default signing algorithm in ccnd is SHA256, so we just need add 32 bytes of garbage
61 static char garbage [DEFAULT_SIGNATURE_SIZE];
Zhenkai Zhu5acebd72013-02-07 19:59:25 -080062
Alexander Afanasyev848a8e42013-02-07 21:19:18 -080063 // digest and witness fields are optional, so use default ones
Zhenkai Zhu5acebd72013-02-07 19:59:25 -080064
65 res |= ccn_charbuf_append_tt(buf, CCN_DTAG_SignatureBits, CCN_DTAG);
Alexander Afanasyev848a8e42013-02-07 21:19:18 -080066 res |= ccn_charbuf_append_tt(buf, DEFAULT_SIGNATURE_SIZE, CCN_BLOB);
67 res |= ccn_charbuf_append(buf, garbage, DEFAULT_SIGNATURE_SIZE);
Zhenkai Zhu5acebd72013-02-07 19:59:25 -080068 res |= ccn_charbuf_append_closer(buf);
Alexander Afanasyev848a8e42013-02-07 21:19:18 -080069
Zhenkai Zhu5acebd72013-02-07 19:59:25 -080070 res |= ccn_charbuf_append_closer(buf);
71
72 return(res == 0 ? 0 : -1);
73}
74
75static int
Alexander Afanasyev848a8e42013-02-07 21:19:18 -080076ccn_pack_unsigned_ContentObject(struct ccn_charbuf *buf,
77 const struct ccn_charbuf *Name,
78 const struct ccn_charbuf *SignedInfo,
79 const void *data,
80 size_t size)
Zhenkai Zhu5acebd72013-02-07 19:59:25 -080081{
82 int res = 0;
Zhenkai Zhu5acebd72013-02-07 19:59:25 -080083 struct ccn_charbuf *content_header;
84 size_t closer_start;
85
86 content_header = ccn_charbuf_create();
87 res |= ccn_charbuf_append_tt(content_header, CCN_DTAG_Content, CCN_DTAG);
88 if (size != 0)
89 res |= ccn_charbuf_append_tt(content_header, size, CCN_BLOB);
90 closer_start = content_header->length;
91 res |= ccn_charbuf_append_closer(content_header);
92 if (res < 0)
93 return(-1);
Alexander Afanasyev848a8e42013-02-07 21:19:18 -080094
Zhenkai Zhu5acebd72013-02-07 19:59:25 -080095 res |= ccn_charbuf_append_tt(buf, CCN_DTAG_ContentObject, CCN_DTAG);
96
Alexander Afanasyev848a8e42013-02-07 21:19:18 -080097 res |= ccn_encode_garbage_Signature(buf);
98
Zhenkai Zhu5acebd72013-02-07 19:59:25 -080099 res |= ccn_charbuf_append_charbuf(buf, Name);
100 res |= ccn_charbuf_append_charbuf(buf, SignedInfo);
101 res |= ccnb_append_tagged_blob(buf, CCN_DTAG_Content, data, size);
102 res |= ccn_charbuf_append_closer(buf);
Alexander Afanasyev848a8e42013-02-07 21:19:18 -0800103
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800104 ccn_charbuf_destroy(&content_header);
105 return(res == 0 ? 0 : -1);
106}
107
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800108CcnxWrapper::CcnxWrapper()
109 : m_handle (0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800110 , m_running (true)
111 , m_connected (false)
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800112 , m_executor (new Executor(1))
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800113{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800114 start ();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800115}
116
117void
118CcnxWrapper::connectCcnd()
119{
Alexander Afanasyev17ae9e32013-02-19 18:09:08 -0800120 if (m_handle != 0) {
121 ccn_disconnect (m_handle);
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800122 //ccn_destroy (&m_handle);
Alexander Afanasyev17ae9e32013-02-19 18:09:08 -0800123 }
124 else
125 {
126 m_handle = ccn_create ();
127 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800128
Alexander Afanasyev17ae9e32013-02-19 18:09:08 -0800129 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800130 if (ccn_connect(m_handle, NULL) < 0)
131 {
132 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
133 }
134 m_connected = true;
135
Alexander Afanasyev17ae9e32013-02-19 18:09:08 -0800136 if (!m_registeredInterests.empty())
137 {
138 for (map<Name, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it)
139 {
140 clearInterestFilter(it->first, false);
141 setInterestFilter(it->first, it->second, false);
142 }
143 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800144}
145
146CcnxWrapper::~CcnxWrapper()
147{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800148 shutdown ();
149}
150
151void
152CcnxWrapper::start () // called automatically in constructor
153{
154 connectCcnd();
155 m_thread = thread (&CcnxWrapper::ccnLoop, this);
156 m_executor->start();
157}
158
159void
160CcnxWrapper::shutdown () // called in destructor, but can called manually
161{
162 m_executor->shutdown();
163
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800164 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800165 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800166 m_running = false;
167 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800168
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800169 _LOG_DEBUG ("+++++++++SHUTDOWN+++++++");
170 if (m_connected)
171 {
172 m_thread.join ();
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800173
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800174 ccn_disconnect (m_handle);
175 //ccn_destroy (&m_handle);
176 m_connected = false;
177 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800178}
179
180void
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800181CcnxWrapper::ccnLoop ()
182{
183 static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0)));
184 static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000));
185
186 while (m_running)
187 {
188 try
189 {
190 int res = 0;
191 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800192 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800193 res = ccn_run (m_handle, 0);
194 }
195
196 if (!m_running) break;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800197
Alexander Afanasyev9c9c3012013-01-25 23:44:25 -0800198 if (res < 0) {
199 _LOG_ERROR ("ccn_run returned negative status: " << res);
Alexander Afanasyev17ae9e32013-02-19 18:09:08 -0800200
201 BOOST_THROW_EXCEPTION (CcnxOperationException()
202 << errmsg_info_str("ccn_run returned error"));
Alexander Afanasyev9c9c3012013-01-25 23:44:25 -0800203 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800204
205
206 pollfd pfds[1];
207 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800208 UniqueRecLock lock(m_mutex);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800209
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800210 pfds[0].fd = ccn_get_connection_fd (m_handle);
211 pfds[0].events = POLLIN;
212 if (ccn_output_is_pending (m_handle))
213 pfds[0].events |= POLLOUT;
214 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800215
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800216 int ret = poll (pfds, 1, 1);
217 if (ret < 0)
218 {
219 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
220 }
221 }
222 catch (CcnxOperationException &e)
223 {
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800224 m_connected = false;
225 // probably ccnd has been stopped
226 // try reconnect with sleep
227 int interval = 1;
228 int maxInterval = 32;
229 while (m_running)
230 {
231 try
232 {
Alexander Afanasyev17ae9e32013-02-19 18:09:08 -0800233 this_thread::sleep (boost::get_system_time () + boost::posix_time::seconds (interval) + boost::posix_time::milliseconds (rangeUniformRandom ()));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800234
Alexander Afanasyev17ae9e32013-02-19 18:09:08 -0800235 connectCcnd ();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800236 _LOG_DEBUG("reconnect to ccnd succeeded");
237 break;
238 }
239 catch (CcnxOperationException &e)
240 {
Alexander Afanasyev17ae9e32013-02-19 18:09:08 -0800241 this_thread::sleep (boost::get_system_time () + boost::posix_time::seconds (interval) + boost::posix_time::milliseconds (rangeUniformRandom ()));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800242
243 // do exponential backup for reconnect interval
244 if (interval < maxInterval)
245 {
246 interval *= 2;
247 }
248 }
249 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800250 }
251 catch (const std::exception &exc)
252 {
253 // catch anything thrown within try block that derives from std::exception
254 std::cerr << exc.what();
255 }
256 catch (...)
257 {
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800258 cout << "UNKNOWN EXCEPTION !!!" << endl;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800259 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800260 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800261}
262
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800263Bytes
Zhenkai Zhu704a6582013-03-13 16:44:42 -0700264CcnxWrapper::createContentObject(const Name &name, const void *buf, size_t len, int freshness, const Name &keyNameParam)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800265{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800266 {
267 UniqueRecLock lock(m_mutex);
268 if (!m_running || !m_connected)
269 {
270 _LOG_TRACE ("<< not running or connected");
271 return Bytes ();
272 }
273 }
274
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800275 CcnxCharbufPtr ptr = name.toCcnxCharbuf();
276 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800277 ccn_charbuf *content = ccn_charbuf_create();
278
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800279 struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
280 sp.freshness = freshness;
281
Zhenkai Zhu704a6582013-03-13 16:44:42 -0700282 Name keyName;
283
284 if (keyNameParam.size() == 0)
Zhenkai Zhu589be8e2013-02-24 20:51:12 -0800285 {
Zhenkai Zhu704a6582013-03-13 16:44:42 -0700286 // use default key name
287 CcnxCharbufPtr defaultKeyNamePtr = boost::make_shared<CcnxCharbuf>();
288 ccn_get_public_key_and_name(m_handle, &sp, NULL, NULL, defaultKeyNamePtr->getBuf());
289 keyName = Name(*defaultKeyNamePtr);
Zhenkai Zhu589be8e2013-02-24 20:51:12 -0800290 }
Zhenkai Zhu704a6582013-03-13 16:44:42 -0700291 else
292 {
293 keyName = keyNameParam;
294 }
295
296 if (sp.template_ccnb == NULL)
297 {
298 sp.template_ccnb = ccn_charbuf_create();
299 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG);
300 }
301 // no idea what the following 3 lines do, but it was there
302 else if (sp.template_ccnb->length > 0) {
303 sp.template_ccnb->length--;
304 }
305 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyLocator, CCN_DTAG);
306 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyName, CCN_DTAG);
307 CcnxCharbufPtr keyPtr = keyName.toCcnxCharbuf();
308 ccn_charbuf *keyBuf = keyPtr->getBuf();
309 ccn_charbuf_append(sp.template_ccnb, keyBuf->buf, keyBuf->length);
310 ccn_charbuf_append_closer(sp.template_ccnb); // </KeyName>
311 ccn_charbuf_append_closer(sp.template_ccnb); // </KeyLocator>
312 sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR;
313 ccn_charbuf_append_closer(sp.template_ccnb); // </SignedInfo>
Zhenkai Zhu589be8e2013-02-24 20:51:12 -0800314
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800315 if (ccn_sign_content(m_handle, content, pname, &sp, buf, len) != 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800316 {
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800317 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("sign content failed"));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800318 }
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800319
320 Bytes bytes;
321 readRaw(bytes, content->buf, content->length);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800322
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800323 ccn_charbuf_destroy (&content);
Zhenkai Zhu589be8e2013-02-24 20:51:12 -0800324 if (sp.template_ccnb != NULL)
325 {
326 ccn_charbuf_destroy (&sp.template_ccnb);
327 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800328
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800329 return bytes;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800330}
331
332int
Zhenkai Zhubad089c2012-12-28 10:28:27 -0800333CcnxWrapper::putToCcnd (const Bytes &contentObject)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800334{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800335 _LOG_TRACE (">> putToCcnd");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800336 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800337 if (!m_running || !m_connected)
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800338 {
339 _LOG_TRACE ("<< not running or connected");
340 return -1;
341 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800342
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800343
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800344 if (ccn_put(m_handle, head(contentObject), contentObject.size()) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800345 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800346 _LOG_ERROR ("ccn_put failed");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800347 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
348 }
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800349 else
350 {
351 _LOG_DEBUG ("<< putToCcnd");
352 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800353
354 return 0;
355}
356
357int
Zhenkai Zhu589be8e2013-02-24 20:51:12 -0800358CcnxWrapper::publishData (const Name &name, const unsigned char *buf, size_t len, int freshness, const Name &keyName)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800359{
Zhenkai Zhu589be8e2013-02-24 20:51:12 -0800360 Bytes co = createContentObject(name, buf, len, freshness, keyName);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800361 return putToCcnd(co);
362}
363
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800364int
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800365CcnxWrapper::publishUnsignedData(const Name &name, const unsigned char *buf, size_t len, int freshness)
366{
367 {
368 UniqueRecLock lock(m_mutex);
369 if (!m_running || !m_connected)
370 {
371 _LOG_TRACE ("<< not running or connected");
372 return -1;
373 }
374 }
375
376 CcnxCharbufPtr ptr = name.toCcnxCharbuf();
377 ccn_charbuf *pname = ptr->getBuf();
378 ccn_charbuf *content = ccn_charbuf_create();
379 ccn_charbuf *signed_info = ccn_charbuf_create();
380
Alexander Afanasyev848a8e42013-02-07 21:19:18 -0800381 static char fakeKey[PUBLISHER_KEY_SIZE];
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800382
383 int res = ccn_signed_info_create(signed_info,
Alexander Afanasyev848a8e42013-02-07 21:19:18 -0800384 fakeKey, PUBLISHER_KEY_SIZE,
385 NULL,
386 CCN_CONTENT_DATA,
387 freshness,
388 NULL,
389 NULL // ccnd is happy with absent key locator and key itself... ha ha
390 );
391 ccn_pack_unsigned_ContentObject(content, pname, signed_info, buf, len);
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800392
393 Bytes bytes;
394 readRaw(bytes, content->buf, content->length);
395
396 ccn_charbuf_destroy (&content);
397 ccn_charbuf_destroy (&signed_info);
398
399 return putToCcnd (bytes);
400}
401
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800402
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800403static void
404deleterInInterestTuple (tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> *tuple)
405{
406 delete tuple->get<0> ();
407 delete tuple;
408}
409
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800410static ccn_upcall_res
411incomingInterest(ccn_closure *selfp,
412 ccn_upcall_kind kind,
413 ccn_upcall_info *info)
414{
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800415 CcnxWrapper::InterestCallback *f;
416 ExecutorPtr executor;
417 tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> *realData = reinterpret_cast< tuple<CcnxWrapper::InterestCallback *, ExecutorPtr>* > (selfp->data);
418 tie (f, executor) = *realData;
419
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800420 switch (kind)
421 {
422 case CCN_UPCALL_FINAL: // effective in unit tests
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800423 // delete closure;
424 executor->execute (bind (deleterInInterestTuple, realData));
425
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800426 delete selfp;
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800427 _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_FINAL");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800428 return CCN_UPCALL_RESULT_OK;
429
430 case CCN_UPCALL_INTEREST:
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800431 _LOG_TRACE (">> incomingInterest upcall: " << Name(info->interest_ccnb, info->interest_comps));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800432 break;
433
434 default:
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800435 _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_RESULT_OK: " << Name(info->interest_ccnb, info->interest_comps));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800436 return CCN_UPCALL_RESULT_OK;
437 }
438
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800439 Name interest(info->interest_ccnb, info->interest_comps);
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800440 Selectors selectors(info->pi);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800441
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800442 executor->execute (bind (*f, interest, selectors));
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800443 // this will be run in executor
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800444 // (*f) (interest);
445 // closure->runInterestCallback(interest);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800446
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800447 return CCN_UPCALL_RESULT_OK;
448}
449
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800450static void
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800451deleterInDataTuple (tuple<Closure *, ExecutorPtr, Selectors> *tuple)
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800452{
453 delete tuple->get<0> ();
454 delete tuple;
455}
456
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800457static ccn_upcall_res
458incomingData(ccn_closure *selfp,
459 ccn_upcall_kind kind,
460 ccn_upcall_info *info)
461{
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800462 // Closure *cp = static_cast<Closure *> (selfp->data);
463 Closure *cp;
464 ExecutorPtr executor;
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800465 Selectors selectors;
466 tuple<Closure *, ExecutorPtr, Selectors> *realData = reinterpret_cast< tuple<Closure*, ExecutorPtr, Selectors>* > (selfp->data);
467 tie (cp, executor, selectors) = *realData;
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800468
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -0700469 bool checked = false;
Zhenkai Zhu79264a42013-02-07 21:49:42 -0800470
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800471 switch (kind)
472 {
473 case CCN_UPCALL_FINAL: // effecitve in unit tests
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800474 executor->execute (bind (deleterInDataTuple, realData));
475
Zhenkai Zhu19f81de2013-01-04 22:27:47 -0800476 cp = NULL;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800477 delete selfp;
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800478 _LOG_TRACE ("<< incomingData with CCN_UPCALL_FINAL");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800479 return CCN_UPCALL_RESULT_OK;
480
481 case CCN_UPCALL_CONTENT:
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -0700482 checked = true;
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800483 _LOG_TRACE (">> incomingData content upcall: " << Name (info->content_ccnb, info->content_comps));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800484 break;
485
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800486 case CCN_UPCALL_CONTENT_UNVERIFIED:
487 _LOG_TRACE (">> incomingData content unverified upcall: " << Name (info->content_ccnb, info->content_comps));
488 break;
489
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800490 case CCN_UPCALL_INTEREST_TIMED_OUT: {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800491 if (cp != NULL)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800492 {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800493 Name interest(info->interest_ccnb, info->interest_comps);
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800494 _LOG_TRACE ("<< incomingData timeout: " << Name (info->interest_ccnb, info->interest_comps));
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800495 executor->execute (bind (&Closure::runTimeoutCallback, cp, interest, *cp, selectors));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800496 }
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800497 else
498 {
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800499 _LOG_TRACE ("<< incomingData timeout, but callback is not set...: " << Name (info->interest_ccnb, info->interest_comps));
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800500 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800501 return CCN_UPCALL_RESULT_OK;
502 }
503
504 default:
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800505 _LOG_TRACE(">> unknown upcall type");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800506 return CCN_UPCALL_RESULT_OK;
507 }
508
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -0700509 PcoPtr pco = make_shared<ParsedContentObject> (info->content_ccnb, info->pco->offset[CCN_PCO_E], checked);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800510
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800511 // this will be run in executor
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800512 executor->execute (bind (&Closure::runDataCallback, cp, pco->name (), pco));
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800513 _LOG_TRACE (">> incomingData");
514
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800515 return CCN_UPCALL_RESULT_OK;
516}
517
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800518int CcnxWrapper::sendInterest (const Name &interest, const Closure &closure, const Selectors &selectors)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800519{
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800520 _LOG_TRACE (">> sendInterest: " << interest);
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800521 {
522 UniqueRecLock lock(m_mutex);
523 if (!m_running || !m_connected)
Alexander Afanasyevb3d4cd52013-01-28 11:20:52 -0800524 {
525 _LOG_ERROR ("<< sendInterest: not running or connected");
526 return -1;
527 }
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800528 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800529
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800530 CcnxCharbufPtr namePtr = interest.toCcnxCharbuf();
531 ccn_charbuf *pname = namePtr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800532 ccn_closure *dataClosure = new ccn_closure;
533
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800534 // Closure *myClosure = new ExecutorClosure(closure, m_executor);
535 Closure *myClosure = closure.dup ();
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800536 dataClosure->data = new tuple<Closure*, ExecutorPtr, Selectors> (myClosure, m_executor, selectors);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800537
538 dataClosure->p = &incomingData;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800539
540 CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf();
541 ccn_charbuf *templ = NULL;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800542 if (selectorsPtr)
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800543 {
544 templ = selectorsPtr->getBuf();
545 }
546
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800547 UniqueRecLock lock(m_mutex);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800548 if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800549 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800550 _LOG_ERROR ("<< sendInterest: ccn_express_interest FAILED!!!");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800551 }
552
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800553 return 0;
554}
555
Alexander Afanasyev17ae9e32013-02-19 18:09:08 -0800556int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback, bool record/* = true*/)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800557{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800558 _LOG_TRACE (">> setInterestFilter");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800559 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800560 if (!m_running || !m_connected)
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800561 {
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800562 return -1;
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800563 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800564
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800565 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
566 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800567 ccn_closure *interestClosure = new ccn_closure;
568
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800569 // interestClosure->data = new ExecutorInterestClosure(interestCallback, m_executor);
570
571 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 -0800572 interestClosure->p = &incomingInterest;
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800573
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800574 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
575 if (ret < 0)
576 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800577 _LOG_ERROR ("<< setInterestFilter: ccn_set_interest_filter FAILED");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800578 }
579
Alexander Afanasyev17ae9e32013-02-19 18:09:08 -0800580 if (record)
581 {
582 m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback));
583 }
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800584
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800585 _LOG_TRACE ("<< setInterestFilter");
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800586
587 return ret;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800588}
589
590void
Alexander Afanasyev17ae9e32013-02-19 18:09:08 -0800591CcnxWrapper::clearInterestFilter (const Name &prefix, bool record/* = true*/)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800592{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800593 _LOG_TRACE (">> clearInterestFilter");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800594 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800595 if (!m_running || !m_connected)
596 return;
597
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800598 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
599 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800600
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800601 int ret = ccn_set_interest_filter (m_handle, pname, 0);
602 if (ret < 0)
603 {
604 }
605
Alexander Afanasyev17ae9e32013-02-19 18:09:08 -0800606 if (record)
607 {
608 m_registeredInterests.erase(prefix);
609 }
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800610
611 _LOG_TRACE ("<< clearInterestFilter");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800612}
613
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800614Name
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800615CcnxWrapper::getLocalPrefix ()
616{
617 struct ccn * tmp_handle = ccn_create ();
618 int res = ccn_connect (tmp_handle, NULL);
619 if (res < 0)
620 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800621 return Name();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800622 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800623
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800624 string retval = "";
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800625
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800626 struct ccn_charbuf *templ = ccn_charbuf_create();
627 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
628 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
629 ccn_charbuf_append_closer(templ); /* </Name> */
630 // XXX - use pubid if possible
631 ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG);
632 ccnb_append_number(templ, 1);
633 ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */
634 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2);
635 ccn_charbuf_append_closer(templ); /* </Interest> */
636
637 struct ccn_charbuf *name = ccn_charbuf_create ();
638 res = ccn_name_from_uri (name, "/local/ndn/prefix");
639 if (res < 0) {
640 }
641 else
642 {
643 struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800644
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800645 struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix",
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800646 NULL, 4, CCN_V_HIGHEST, 0);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800647 if (stream == NULL) {
648 }
649 else
650 {
651 ostringstream os;
652
653 int counter = 0;
654 char buf[256];
655 while (true) {
656 res = ccn_fetch_read (stream, buf, sizeof(buf));
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800657
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800658 if (res == 0) {
659 break;
660 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800661
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800662 if (res > 0) {
663 os << string(buf, res);
664 } else if (res == CCN_FETCH_READ_NONE) {
665 if (counter < 2)
666 {
667 ccn_run(tmp_handle, 1000);
668 counter ++;
669 }
670 else
671 {
672 break;
673 }
674 } else if (res == CCN_FETCH_READ_END) {
675 break;
676 } else if (res == CCN_FETCH_READ_TIMEOUT) {
677 break;
678 } else {
679 break;
680 }
681 }
682 retval = os.str ();
683 stream = ccn_fetch_close(stream);
684 }
685 fetch = ccn_fetch_destroy(fetch);
686 }
687
688 ccn_charbuf_destroy (&name);
689
690 ccn_disconnect (tmp_handle);
691 ccn_destroy (&tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800692
Zhenkai Zhucbbb8882013-01-25 13:49:12 -0800693 boost::algorithm::trim(retval);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800694 return Name(retval);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800695}
696
Zhenkai Zhu79264a42013-02-07 21:49:42 -0800697bool
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -0700698CcnxWrapper::checkPcoIntegrity(PcoPtr &pco)
Zhenkai Zhu79264a42013-02-07 21:49:42 -0800699{
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -0700700 bool checked = (ccn_verify_content(m_handle, pco->msg(), (ccn_parsed_ContentObject *)pco->pco()) == 0);
701 pco->setIntegrityChecked(checked);
702 return checked;
Zhenkai Zhu79264a42013-02-07 21:49:42 -0800703}
704
Jared Lindblomf83ff942013-03-01 00:01:49 -0800705// This is needed just for get function implementation
706struct GetState
707{
708 GetState (double maxWait)
709 {
710 double intPart, fraction;
711 fraction = modf (std::abs(maxWait), &intPart);
712
713 m_maxWait = date_time::second_clock<boost::posix_time::ptime>::universal_time()
714 + boost::posix_time::seconds (intPart)
715 + boost::posix_time::microseconds (fraction * 1000000);
716 }
717
718 PcoPtr
719 WaitForResult ()
720 {
721 boost::unique_lock<boost::mutex> lock (m_mutex);
722 m_cond.timed_wait (lock, m_maxWait);
723
724 return m_retval;
725 }
726
727 void
728 DataCallback (Name name, PcoPtr pco)
729 {
730 m_retval = pco;
731
732 boost::unique_lock<boost::mutex> lock (m_mutex);
733 m_cond.notify_one ();
734 }
735
736 void
737 TimeoutCallback (Name name)
738 {
739 boost::unique_lock<boost::mutex> lock (m_mutex);
740 m_cond.notify_one ();
741 }
742
743private:
744 boost::posix_time::ptime m_maxWait;
745
746 boost::mutex m_mutex;
747 boost::condition_variable m_cond;
748
749 PcoPtr m_retval;
750};
751
752
753PcoPtr
754CcnxWrapper::get(const Name &interest, const Selectors &selectors, double maxWait/* = 4.0*/)
755{
756 _LOG_TRACE (">> get: " << interest);
757 {
758 UniqueRecLock lock(m_mutex);
759 if (!m_running || !m_connected)
760 {
761 _LOG_ERROR ("<< get: not running or connected");
762 return PcoPtr ();
763 }
764 }
765
766 GetState state (maxWait);
767 this->sendInterest (interest, Closure (boost::bind (&GetState::DataCallback, &state, _1, _2),
768 boost::bind (&GetState::TimeoutCallback, &state, _1)),
769 selectors);
770 return state.WaitForResult ();
771}
772
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800773}