blob: 58bacc435602d10b3c2f94ee038b8b995d3b3c3b [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 ();
Chaoyi Bian95a58c32012-03-09 15:43:59 -080041 if (ccn_connect(m_handle, NULL) < 0)
42 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080043 initKeyStore();
44 createKeyLocator();
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 Afanasyev172d2b72012-03-08 23:43:39 -080058 ccn_charbuf_destroy (&m_keyLoactor);
59 ccn_keystore_destroy (&m_keyStore);
Alexander Afanasyevc91085d2012-03-13 23:08:27 -070060 ccn_destroy (&m_handle);
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];
110 int res = ccn_run(m_handle, 0);
111
112 pfds[0].fd = ccn_get_connection_fd(m_handle);
113 pfds[0].events = POLLIN;
114
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800115 while (m_running)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800116 {
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800117 if (res >= 0)
118 {
119 int ret = poll(pfds, 1, 100);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700120 if (ret < 0)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800121 {
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700122 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800123 }
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700124
125 recursive_mutex::scoped_lock lock(m_mutex);
126 if (!m_running) break;
127
128 res = ccn_run(m_handle, 0);
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800129 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800130 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800131}
132
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800133/// @endcond
134
135int
136CcnxWrapper::publishData (const string &name, const string &dataBuffer, int freshness)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800137{
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700138 // cout << "Publish: " << name << endl;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800139 ccn_charbuf *pname = ccn_charbuf_create();
140 ccn_charbuf *signed_info = ccn_charbuf_create();
141 ccn_charbuf *content = ccn_charbuf_create();
142
143 ccn_name_from_uri(pname, name.c_str());
144 ccn_signed_info_create(signed_info,
145 getPublicKeyDigest(),
146 getPublicKeyDigestLength(),
147 NULL,
148 CCN_CONTENT_DATA,
149 freshness,
150 NULL,
151 m_keyLoactor);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800152 if(ccn_encode_ContentObject(content, pname, signed_info,
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800153 dataBuffer.c_str(), dataBuffer.length (),
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800154 NULL, getPrivateKey()) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800155 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("encode content failed"));
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800156
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800157 recursive_mutex::scoped_lock lock(m_mutex);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800158 if (ccn_put(m_handle, content->buf, content->length) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800159 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800160
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800161 ccn_charbuf_destroy (&pname);
162 ccn_charbuf_destroy (&signed_info);
163 ccn_charbuf_destroy (&content);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800164}
165
166
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800167static ccn_upcall_res
168incomingInterest(ccn_closure *selfp,
169 ccn_upcall_kind kind,
170 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800171{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800172 CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800173
174 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800175 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800176 case CCN_UPCALL_FINAL: // effective in unit tests
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800177 delete f;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800178 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800179 return CCN_UPCALL_RESULT_OK;
180
181 case CCN_UPCALL_INTEREST:
182 break;
183
184 default:
185 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800186 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800187
Chaoyi Bian93d43102012-03-07 14:28:56 -0800188 string interest;
Chaoyi Bian02dba3c2012-03-07 21:45:22 -0800189 for (int i = 0; i < info->interest_comps->n - 1; i++)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800190 {
191 char *comp;
192 size_t size;
193 interest += "/";
194 ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800195 string compStr(comp, size);
196 interest += compStr;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800197 }
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800198 (*f) (interest);
Chaoyi Bian93d43102012-03-07 14:28:56 -0800199 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800200}
201
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800202static ccn_upcall_res
203incomingData(ccn_closure *selfp,
204 ccn_upcall_kind kind,
205 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800206{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800207 CcnxWrapper::DataCallback *f = static_cast<CcnxWrapper::DataCallback*> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800208
209 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800210 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800211 case CCN_UPCALL_FINAL: // effecitve in unit tests
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800212 delete f;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800213 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800214 return CCN_UPCALL_RESULT_OK;
215
216 case CCN_UPCALL_CONTENT:
217 break;
218
219 default:
220 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800221 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800222
223 char *pcontent;
224 size_t len;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800225 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 -0800226 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
227 string content(pcontent, len);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800228
Chaoyi Bian4194b742012-03-08 17:21:35 -0800229 string name;
230 for (int i = 0; i < info->content_comps->n - 1; i++)
231 {
232 char *comp;
233 size_t size;
234 name += "/";
235 ccn_name_comp_get(info->content_ccnb, info->content_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800236 string compStr(comp, size);
237 name += compStr;
Chaoyi Bian4194b742012-03-08 17:21:35 -0800238 }
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800239 (*f) (name, content);
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800240 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800241}
242
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800243int CcnxWrapper::sendInterest (const string &strInterest, const DataCallback &dataCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800244{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700245 // std::cout << "Send interests for " << strInterest << std::endl;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800246 ccn_charbuf *pname = ccn_charbuf_create();
247 ccn_closure *dataClosure = new ccn_closure;
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800248
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800249 ccn_name_from_uri (pname, strInterest.c_str());
250 dataClosure->data = new DataCallback (dataCallback); // should be removed when closure is removed
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800251
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800252 dataClosure->p = &incomingData;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800253 recursive_mutex::scoped_lock lock(m_mutex);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800254 if (ccn_express_interest (m_handle, pname, dataClosure, NULL) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800255 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("express interest failed"));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800256
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800257 ccn_charbuf_destroy (&pname);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800258}
259
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800260int CcnxWrapper::setInterestFilter (const string &prefix, const InterestCallback &interestCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800261{
262 ccn_charbuf *pname = ccn_charbuf_create();
263 ccn_closure *interestClosure = new ccn_closure;
264
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800265 ccn_name_from_uri (pname, prefix.c_str());
266 interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800267 interestClosure->p = &incomingInterest;
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700268 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
269 if (ret < 0)
270 {
271 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret));
272 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800273
274 ccn_charbuf_destroy(&pname);
275}
276
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700277void
278CcnxWrapper::clearInterestFilter (const std::string &prefix)
279{
280 std::cout << "clearInterestFilter" << std::endl;
281 ccn_charbuf *pname = ccn_charbuf_create();
282
283 ccn_name_from_uri (pname, prefix.c_str());
284 int ret = ccn_set_interest_filter (m_handle, pname, 0);
285 if (ret < 0)
286 {
287 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret));
288 }
289
290 ccn_charbuf_destroy(&pname);
291}
292
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800293}