blob: d5319719704fc0d025c17f1bcc89615a6ae17b0c [file] [log] [blame]
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 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 * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
20 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080022
23#include "sync-ccnx-wrapper.h"
24#include <poll.h>
Zhenkai Zhu009ff792012-03-09 12:37:52 -080025#include <boost/throw_exception.hpp>
Chaoyi Bian95a58c32012-03-09 15:43:59 -080026typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
Alexander Afanasyev387ac952012-03-11 23:49:27 -070027typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080028
29using namespace std;
30using namespace boost;
31
32namespace Sync {
33
Alexander Afanasyev2247b302012-03-14 14:11:54 -070034#ifdef _DEBUG_WRAPPER_
35CcnxWrapper::CcnxWrapper(char c)
36#else
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080037CcnxWrapper::CcnxWrapper()
Alexander Afanasyev2247b302012-03-14 14:11:54 -070038#endif
Alexander Afanasyev1285b382012-03-08 16:40:27 -080039 : m_handle (0)
40 , m_keyStore (0)
41 , m_keyLoactor (0)
42 , m_running (true)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080043{
Alexander Afanasyev2247b302012-03-14 14:11:54 -070044#ifdef _DEBUG_WRAPPER_
45 m_c = c;
46#endif
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070047 m_handle = ccn_create ();
Alexander Afanasyev2c180772012-03-13 23:58:46 -070048 initKeyStore ();
49 createKeyLocator ();
Chaoyi Bian95a58c32012-03-09 15:43:59 -080050 if (ccn_connect(m_handle, NULL) < 0)
51 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
Alexander Afanasyev1285b382012-03-08 16:40:27 -080052 m_thread = thread (&CcnxWrapper::ccnLoop, this);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080053}
54
55CcnxWrapper::~CcnxWrapper()
56{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070057 // std::cout << "CcnxWrapper::~CcnxWrapper()" << std::endl;
58 {
59 recursive_mutex::scoped_lock lock(m_mutex);
60 m_running = false;
61 }
62
63 m_thread.join ();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080064 ccn_disconnect (m_handle);
Alexander Afanasyev2c180772012-03-13 23:58:46 -070065 ccn_destroy (&m_handle);
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080066 ccn_charbuf_destroy (&m_keyLoactor);
67 ccn_keystore_destroy (&m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080068}
69
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080070/// @cond include_hidden
71
72void
73CcnxWrapper::createKeyLocator ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080074{
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080075 m_keyLoactor = ccn_charbuf_create();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080076 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG);
77 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_Key, CCN_DTAG);
78 int res = ccn_append_pubkey_blob (m_keyLoactor, ccn_keystore_public_key(m_keyStore));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080079 if (res >= 0)
Alexander Afanasyev1285b382012-03-08 16:40:27 -080080 {
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080081 ccn_charbuf_append_closer (m_keyLoactor); /* </Key> */
82 ccn_charbuf_append_closer (m_keyLoactor); /* </KeyLocator> */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080083 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080084}
85
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080086const ccn_pkey*
87CcnxWrapper::getPrivateKey ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080088{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080089 return ccn_keystore_private_key (m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080090}
91
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080092const unsigned char*
93CcnxWrapper::getPublicKeyDigest ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080094{
95 return ccn_keystore_public_key_digest(m_keyStore);
96}
97
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080098ssize_t
99CcnxWrapper::getPublicKeyDigestLength ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800100{
101 return ccn_keystore_public_key_digest_length(m_keyStore);
102}
103
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800104void
105CcnxWrapper::initKeyStore ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800106{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800107 m_keyStore = ccn_keystore_create ();
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800108 string keyStoreFile = string(getenv("HOME")) + string("/.ccnx/.ccnx_keystore");
109 if (ccn_keystore_init (m_keyStore, (char *)keyStoreFile.c_str(), (char*)"Th1s1sn0t8g00dp8ssw0rd.") < 0)
110 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str(keyStoreFile.c_str()));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800111}
112
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800113void
114CcnxWrapper::ccnLoop ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800115{
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800116
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800117 while (m_running)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800118 {
Alexander Afanasyev2247b302012-03-14 14:11:54 -0700119#ifdef _DEBUG_WRAPPER_
120 std::cout << m_c << flush;
121#endif
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700122 int res = 0;
123 {
124 recursive_mutex::scoped_lock lock (m_mutex);
125 res = ccn_run (m_handle, 0);
Alexander Afanasyev2247b302012-03-14 14:11:54 -0700126
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700127 }
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700128
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700129 if (!m_running) break;
130
131 if (res < 0)
132 BOOST_THROW_EXCEPTION (CcnxOperationException()
133 << errmsg_info_str("ccn_run returned error"));
134
Alexander Afanasyev2247b302012-03-14 14:11:54 -0700135
136 pollfd pfds[1];
137 {
138 recursive_mutex::scoped_lock lock (m_mutex);
139
140 pfds[0].fd = ccn_get_connection_fd (m_handle);
141 pfds[0].events = POLLIN;
142 if (ccn_output_is_pending (m_handle))
143 pfds[0].events |= POLLOUT;
144 }
145
146 int ret = poll (pfds, 1, 1);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700147 if (ret < 0)
148 {
149 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800150 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800151 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800152}
153
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800154/// @endcond
155
156int
157CcnxWrapper::publishData (const string &name, const string &dataBuffer, int freshness)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800158{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700159 recursive_mutex::scoped_lock lock(m_mutex);
160 if (!m_running)
161 return -1;
162
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700163 // cout << "Publish: " << name << endl;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800164 ccn_charbuf *pname = ccn_charbuf_create();
165 ccn_charbuf *signed_info = ccn_charbuf_create();
166 ccn_charbuf *content = ccn_charbuf_create();
167
168 ccn_name_from_uri(pname, name.c_str());
169 ccn_signed_info_create(signed_info,
170 getPublicKeyDigest(),
171 getPublicKeyDigestLength(),
172 NULL,
173 CCN_CONTENT_DATA,
174 freshness,
175 NULL,
176 m_keyLoactor);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800177 if(ccn_encode_ContentObject(content, pname, signed_info,
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800178 dataBuffer.c_str(), dataBuffer.length (),
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800179 NULL, getPrivateKey()) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800180 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("encode content failed"));
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800181
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800182 if (ccn_put(m_handle, content->buf, content->length) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800183 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800184
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800185 ccn_charbuf_destroy (&pname);
186 ccn_charbuf_destroy (&signed_info);
187 ccn_charbuf_destroy (&content);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700188 return 0;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800189}
190
191
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800192static ccn_upcall_res
193incomingInterest(ccn_closure *selfp,
194 ccn_upcall_kind kind,
195 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800196{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800197 CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800198
199 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800200 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800201 case CCN_UPCALL_FINAL: // effective in unit tests
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800202 delete f;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800203 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800204 return CCN_UPCALL_RESULT_OK;
205
206 case CCN_UPCALL_INTEREST:
207 break;
208
209 default:
210 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800211 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800212
Chaoyi Bian93d43102012-03-07 14:28:56 -0800213 string interest;
Chaoyi Bian02dba3c2012-03-07 21:45:22 -0800214 for (int i = 0; i < info->interest_comps->n - 1; i++)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800215 {
216 char *comp;
217 size_t size;
218 interest += "/";
219 ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800220 string compStr(comp, size);
221 interest += compStr;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800222 }
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800223 (*f) (interest);
Chaoyi Bian93d43102012-03-07 14:28:56 -0800224 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800225}
226
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800227static ccn_upcall_res
228incomingData(ccn_closure *selfp,
229 ccn_upcall_kind kind,
230 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800231{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800232 CcnxWrapper::DataCallback *f = static_cast<CcnxWrapper::DataCallback*> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800233
234 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800235 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800236 case CCN_UPCALL_FINAL: // effecitve in unit tests
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800237 delete f;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800238 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800239 return CCN_UPCALL_RESULT_OK;
240
241 case CCN_UPCALL_CONTENT:
242 break;
243
244 default:
245 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800246 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800247
248 char *pcontent;
249 size_t len;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800250 if (ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, (const unsigned char **)&pcontent, &len) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800251 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
252 string content(pcontent, len);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800253
Chaoyi Bian4194b742012-03-08 17:21:35 -0800254 string name;
255 for (int i = 0; i < info->content_comps->n - 1; i++)
256 {
257 char *comp;
258 size_t size;
259 name += "/";
260 ccn_name_comp_get(info->content_ccnb, info->content_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800261 string compStr(comp, size);
262 name += compStr;
Chaoyi Bian4194b742012-03-08 17:21:35 -0800263 }
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800264 (*f) (name, content);
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800265 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800266}
267
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800268int CcnxWrapper::sendInterest (const string &strInterest, const DataCallback &dataCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800269{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700270 recursive_mutex::scoped_lock lock(m_mutex);
271 if (!m_running)
272 return -1;
273
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700274 // std::cout << "Send interests for " << strInterest << std::endl;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800275 ccn_charbuf *pname = ccn_charbuf_create();
276 ccn_closure *dataClosure = new ccn_closure;
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800277
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800278 ccn_name_from_uri (pname, strInterest.c_str());
279 dataClosure->data = new DataCallback (dataCallback); // should be removed when closure is removed
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800280
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800281 dataClosure->p = &incomingData;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800282 if (ccn_express_interest (m_handle, pname, dataClosure, NULL) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800283 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("express interest failed"));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800284
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800285 ccn_charbuf_destroy (&pname);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700286 return 0;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800287}
288
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800289int CcnxWrapper::setInterestFilter (const string &prefix, const InterestCallback &interestCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800290{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700291 recursive_mutex::scoped_lock lock(m_mutex);
292 if (!m_running)
293 return -1;
294
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800295 ccn_charbuf *pname = ccn_charbuf_create();
296 ccn_closure *interestClosure = new ccn_closure;
297
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800298 ccn_name_from_uri (pname, prefix.c_str());
299 interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800300 interestClosure->p = &incomingInterest;
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700301 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
302 if (ret < 0)
303 {
304 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret));
305 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800306
307 ccn_charbuf_destroy(&pname);
308}
309
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700310void
311CcnxWrapper::clearInterestFilter (const std::string &prefix)
312{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700313 recursive_mutex::scoped_lock lock(m_mutex);
314 if (!m_running)
315 return;
316
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700317 std::cout << "clearInterestFilter" << std::endl;
318 ccn_charbuf *pname = ccn_charbuf_create();
319
320 ccn_name_from_uri (pname, prefix.c_str());
321 int ret = ccn_set_interest_filter (m_handle, pname, 0);
322 if (ret < 0)
323 {
324 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret));
325 }
326
327 ccn_charbuf_destroy(&pname);
328}
329
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800330}