blob: aa7ab8cfdf7a2c1de184e850c8fa0befa00f0570 [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
34CcnxWrapper::CcnxWrapper()
Alexander Afanasyev1285b382012-03-08 16:40:27 -080035 : m_handle (0)
36 , m_keyStore (0)
37 , m_keyLoactor (0)
38 , m_running (true)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080039{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070040 m_handle = ccn_create ();
Alexander Afanasyev2c180772012-03-13 23:58:46 -070041 initKeyStore ();
42 createKeyLocator ();
Chaoyi Bian95a58c32012-03-09 15:43:59 -080043 if (ccn_connect(m_handle, NULL) < 0)
44 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
Alexander Afanasyev1285b382012-03-08 16:40:27 -080045 m_thread = thread (&CcnxWrapper::ccnLoop, this);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080046}
47
48CcnxWrapper::~CcnxWrapper()
49{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070050 // std::cout << "CcnxWrapper::~CcnxWrapper()" << std::endl;
51 {
52 recursive_mutex::scoped_lock lock(m_mutex);
53 m_running = false;
54 }
55
56 m_thread.join ();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080057 ccn_disconnect (m_handle);
Alexander Afanasyev2c180772012-03-13 23:58:46 -070058 ccn_destroy (&m_handle);
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080059 ccn_charbuf_destroy (&m_keyLoactor);
60 ccn_keystore_destroy (&m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080061}
62
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080063/// @cond include_hidden
64
65void
66CcnxWrapper::createKeyLocator ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080067{
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080068 m_keyLoactor = ccn_charbuf_create();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080069 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG);
70 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_Key, CCN_DTAG);
71 int res = ccn_append_pubkey_blob (m_keyLoactor, ccn_keystore_public_key(m_keyStore));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080072 if (res >= 0)
Alexander Afanasyev1285b382012-03-08 16:40:27 -080073 {
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080074 ccn_charbuf_append_closer (m_keyLoactor); /* </Key> */
75 ccn_charbuf_append_closer (m_keyLoactor); /* </KeyLocator> */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080076 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080077}
78
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080079const ccn_pkey*
80CcnxWrapper::getPrivateKey ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080081{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080082 return ccn_keystore_private_key (m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080083}
84
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080085const unsigned char*
86CcnxWrapper::getPublicKeyDigest ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080087{
88 return ccn_keystore_public_key_digest(m_keyStore);
89}
90
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080091ssize_t
92CcnxWrapper::getPublicKeyDigestLength ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080093{
94 return ccn_keystore_public_key_digest_length(m_keyStore);
95}
96
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080097void
98CcnxWrapper::initKeyStore ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080099{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800100 m_keyStore = ccn_keystore_create ();
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800101 string keyStoreFile = string(getenv("HOME")) + string("/.ccnx/.ccnx_keystore");
102 if (ccn_keystore_init (m_keyStore, (char *)keyStoreFile.c_str(), (char*)"Th1s1sn0t8g00dp8ssw0rd.") < 0)
103 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str(keyStoreFile.c_str()));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800104}
105
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800106void
107CcnxWrapper::ccnLoop ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800108{
109 pollfd pfds[1];
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800110
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700111 pfds[0].fd = ccn_get_connection_fd (m_handle);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800112 pfds[0].events = POLLIN;
113
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800114 while (m_running)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800115 {
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700116 int res = 0;
117 {
118 recursive_mutex::scoped_lock lock (m_mutex);
119 res = ccn_run (m_handle, 0);
120 }
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700121
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700122 if (!m_running) break;
123
124 if (res < 0)
125 BOOST_THROW_EXCEPTION (CcnxOperationException()
126 << errmsg_info_str("ccn_run returned error"));
127
128 int ret = poll(pfds, 1, 10);
129 if (ret < 0)
130 {
131 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800132 }
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700133
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800134 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800135}
136
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800137/// @endcond
138
139int
140CcnxWrapper::publishData (const string &name, const string &dataBuffer, int freshness)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800141{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700142 recursive_mutex::scoped_lock lock(m_mutex);
143 if (!m_running)
144 return -1;
145
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700146 // cout << "Publish: " << name << endl;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800147 ccn_charbuf *pname = ccn_charbuf_create();
148 ccn_charbuf *signed_info = ccn_charbuf_create();
149 ccn_charbuf *content = ccn_charbuf_create();
150
151 ccn_name_from_uri(pname, name.c_str());
152 ccn_signed_info_create(signed_info,
153 getPublicKeyDigest(),
154 getPublicKeyDigestLength(),
155 NULL,
156 CCN_CONTENT_DATA,
157 freshness,
158 NULL,
159 m_keyLoactor);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800160 if(ccn_encode_ContentObject(content, pname, signed_info,
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800161 dataBuffer.c_str(), dataBuffer.length (),
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800162 NULL, getPrivateKey()) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800163 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("encode content failed"));
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800164
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800165 if (ccn_put(m_handle, content->buf, content->length) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800166 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800167
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800168 ccn_charbuf_destroy (&pname);
169 ccn_charbuf_destroy (&signed_info);
170 ccn_charbuf_destroy (&content);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700171 return 0;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800172}
173
174
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800175static ccn_upcall_res
176incomingInterest(ccn_closure *selfp,
177 ccn_upcall_kind kind,
178 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800179{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800180 CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800181
182 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800183 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800184 case CCN_UPCALL_FINAL: // effective in unit tests
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800185 delete f;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800186 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800187 return CCN_UPCALL_RESULT_OK;
188
189 case CCN_UPCALL_INTEREST:
190 break;
191
192 default:
193 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800194 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800195
Chaoyi Bian93d43102012-03-07 14:28:56 -0800196 string interest;
Chaoyi Bian02dba3c2012-03-07 21:45:22 -0800197 for (int i = 0; i < info->interest_comps->n - 1; i++)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800198 {
199 char *comp;
200 size_t size;
201 interest += "/";
202 ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800203 string compStr(comp, size);
204 interest += compStr;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800205 }
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800206 (*f) (interest);
Chaoyi Bian93d43102012-03-07 14:28:56 -0800207 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800208}
209
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800210static ccn_upcall_res
211incomingData(ccn_closure *selfp,
212 ccn_upcall_kind kind,
213 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800214{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800215 CcnxWrapper::DataCallback *f = static_cast<CcnxWrapper::DataCallback*> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800216
217 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800218 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800219 case CCN_UPCALL_FINAL: // effecitve in unit tests
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800220 delete f;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800221 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800222 return CCN_UPCALL_RESULT_OK;
223
224 case CCN_UPCALL_CONTENT:
225 break;
226
227 default:
228 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800229 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800230
231 char *pcontent;
232 size_t len;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800233 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 -0800234 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
235 string content(pcontent, len);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800236
Chaoyi Bian4194b742012-03-08 17:21:35 -0800237 string name;
238 for (int i = 0; i < info->content_comps->n - 1; i++)
239 {
240 char *comp;
241 size_t size;
242 name += "/";
243 ccn_name_comp_get(info->content_ccnb, info->content_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800244 string compStr(comp, size);
245 name += compStr;
Chaoyi Bian4194b742012-03-08 17:21:35 -0800246 }
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800247 (*f) (name, content);
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800248 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800249}
250
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800251int CcnxWrapper::sendInterest (const string &strInterest, const DataCallback &dataCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800252{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700253 recursive_mutex::scoped_lock lock(m_mutex);
254 if (!m_running)
255 return -1;
256
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700257 // std::cout << "Send interests for " << strInterest << std::endl;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800258 ccn_charbuf *pname = ccn_charbuf_create();
259 ccn_closure *dataClosure = new ccn_closure;
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800260
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800261 ccn_name_from_uri (pname, strInterest.c_str());
262 dataClosure->data = new DataCallback (dataCallback); // should be removed when closure is removed
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800263
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800264 dataClosure->p = &incomingData;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800265 if (ccn_express_interest (m_handle, pname, dataClosure, NULL) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800266 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("express interest failed"));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800267
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800268 ccn_charbuf_destroy (&pname);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700269 return 0;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800270}
271
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800272int CcnxWrapper::setInterestFilter (const string &prefix, const InterestCallback &interestCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800273{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700274 recursive_mutex::scoped_lock lock(m_mutex);
275 if (!m_running)
276 return -1;
277
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800278 ccn_charbuf *pname = ccn_charbuf_create();
279 ccn_closure *interestClosure = new ccn_closure;
280
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800281 ccn_name_from_uri (pname, prefix.c_str());
282 interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800283 interestClosure->p = &incomingInterest;
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700284 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
285 if (ret < 0)
286 {
287 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret));
288 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800289
290 ccn_charbuf_destroy(&pname);
291}
292
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700293void
294CcnxWrapper::clearInterestFilter (const std::string &prefix)
295{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700296 recursive_mutex::scoped_lock lock(m_mutex);
297 if (!m_running)
298 return;
299
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700300 std::cout << "clearInterestFilter" << std::endl;
301 ccn_charbuf *pname = ccn_charbuf_create();
302
303 ccn_name_from_uri (pname, prefix.c_str());
304 int ret = ccn_set_interest_filter (m_handle, pname, 0);
305 if (ret < 0)
306 {
307 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret));
308 }
309
310 ccn_charbuf_destroy(&pname);
311}
312
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800313}