blob: 6b25dfaa786bfa5c2e2802c2ff2e184a374953cc [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;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080027
28using namespace std;
29using namespace boost;
30
31namespace Sync {
32
33CcnxWrapper::CcnxWrapper()
Alexander Afanasyev1285b382012-03-08 16:40:27 -080034 : m_handle (0)
35 , m_keyStore (0)
36 , m_keyLoactor (0)
37 , m_running (true)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080038{
39 m_handle = ccn_create();
Chaoyi Bian95a58c32012-03-09 15:43:59 -080040 if (ccn_connect(m_handle, NULL) < 0)
41 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080042 initKeyStore();
43 createKeyLocator();
Alexander Afanasyev1285b382012-03-08 16:40:27 -080044 m_thread = thread (&CcnxWrapper::ccnLoop, this);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080045}
46
47CcnxWrapper::~CcnxWrapper()
48{
Alexander Afanasyev1285b382012-03-08 16:40:27 -080049 m_running = false;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080050 m_thread.join();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080051 ccn_disconnect (m_handle);
52 ccn_destroy (&m_handle);
53 ccn_charbuf_destroy (&m_keyLoactor);
54 ccn_keystore_destroy (&m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080055}
56
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080057/// @cond include_hidden
58
59void
60CcnxWrapper::createKeyLocator ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080061{
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080062 m_keyLoactor = ccn_charbuf_create();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080063 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG);
64 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_Key, CCN_DTAG);
65 int res = ccn_append_pubkey_blob (m_keyLoactor, ccn_keystore_public_key(m_keyStore));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080066 if (res >= 0)
Alexander Afanasyev1285b382012-03-08 16:40:27 -080067 {
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080068 ccn_charbuf_append_closer (m_keyLoactor); /* </Key> */
69 ccn_charbuf_append_closer (m_keyLoactor); /* </KeyLocator> */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080070 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080071}
72
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080073const ccn_pkey*
74CcnxWrapper::getPrivateKey ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080075{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080076 return ccn_keystore_private_key (m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080077}
78
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080079const unsigned char*
80CcnxWrapper::getPublicKeyDigest ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080081{
82 return ccn_keystore_public_key_digest(m_keyStore);
83}
84
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080085ssize_t
86CcnxWrapper::getPublicKeyDigestLength ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080087{
88 return ccn_keystore_public_key_digest_length(m_keyStore);
89}
90
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080091void
92CcnxWrapper::initKeyStore ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080093{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080094 m_keyStore = ccn_keystore_create ();
Chaoyi Bian95a58c32012-03-09 15:43:59 -080095 string keyStoreFile = string(getenv("HOME")) + string("/.ccnx/.ccnx_keystore");
96 if (ccn_keystore_init (m_keyStore, (char *)keyStoreFile.c_str(), (char*)"Th1s1sn0t8g00dp8ssw0rd.") < 0)
97 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str(keyStoreFile.c_str()));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080098}
99
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800100void
101CcnxWrapper::ccnLoop ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800102{
103 pollfd pfds[1];
104 int res = ccn_run(m_handle, 0);
105
106 pfds[0].fd = ccn_get_connection_fd(m_handle);
107 pfds[0].events = POLLIN;
108
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800109 while (m_running)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800110 {
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800111 if (res >= 0)
112 {
113 int ret = poll(pfds, 1, 100);
114 if (ret >= 0)
115 {
116 recursive_mutex::scoped_lock lock(m_mutex);
117 res = ccn_run(m_handle, 0);
118 }
119 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800120 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800121}
122
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800123/// @endcond
124
125int
126CcnxWrapper::publishData (const string &name, const string &dataBuffer, int freshness)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800127{
128 ccn_charbuf *pname = ccn_charbuf_create();
129 ccn_charbuf *signed_info = ccn_charbuf_create();
130 ccn_charbuf *content = ccn_charbuf_create();
131
132 ccn_name_from_uri(pname, name.c_str());
133 ccn_signed_info_create(signed_info,
134 getPublicKeyDigest(),
135 getPublicKeyDigestLength(),
136 NULL,
137 CCN_CONTENT_DATA,
138 freshness,
139 NULL,
140 m_keyLoactor);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800141 if(ccn_encode_ContentObject(content, pname, signed_info,
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800142 dataBuffer.c_str(), dataBuffer.length (),
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800143 NULL, getPrivateKey()) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800144 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("encode content failed"));
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800145
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800146 recursive_mutex::scoped_lock lock(m_mutex);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800147 if (ccn_put(m_handle, content->buf, content->length) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800148 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800149
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800150 ccn_charbuf_destroy (&pname);
151 ccn_charbuf_destroy (&signed_info);
152 ccn_charbuf_destroy (&content);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800153}
154
155
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800156static ccn_upcall_res
157incomingInterest(ccn_closure *selfp,
158 ccn_upcall_kind kind,
159 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800160{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800161 CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800162
163 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800164 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800165 case CCN_UPCALL_FINAL: // effective in unit tests
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800166 delete f;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800167 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800168 return CCN_UPCALL_RESULT_OK;
169
170 case CCN_UPCALL_INTEREST:
171 break;
172
173 default:
174 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800175 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800176
Chaoyi Bian93d43102012-03-07 14:28:56 -0800177 string interest;
Chaoyi Bian02dba3c2012-03-07 21:45:22 -0800178 for (int i = 0; i < info->interest_comps->n - 1; i++)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800179 {
180 char *comp;
181 size_t size;
182 interest += "/";
183 ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800184 string compStr(comp, size);
185 interest += compStr;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800186 }
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800187 (*f) (interest);
Chaoyi Bian93d43102012-03-07 14:28:56 -0800188 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800189}
190
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800191static ccn_upcall_res
192incomingData(ccn_closure *selfp,
193 ccn_upcall_kind kind,
194 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800195{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800196 CcnxWrapper::DataCallback *f = static_cast<CcnxWrapper::DataCallback*> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800197
198 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800199 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800200 case CCN_UPCALL_FINAL: // effecitve in unit tests
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800201 delete f;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800202 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800203 return CCN_UPCALL_RESULT_OK;
204
205 case CCN_UPCALL_CONTENT:
206 break;
207
208 default:
209 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800210 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800211
212 char *pcontent;
213 size_t len;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800214 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 -0800215 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
216 string content(pcontent, len);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800217
Chaoyi Bian4194b742012-03-08 17:21:35 -0800218 string name;
219 for (int i = 0; i < info->content_comps->n - 1; i++)
220 {
221 char *comp;
222 size_t size;
223 name += "/";
224 ccn_name_comp_get(info->content_ccnb, info->content_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800225 string compStr(comp, size);
226 name += compStr;
Chaoyi Bian4194b742012-03-08 17:21:35 -0800227 }
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800228 (*f) (name, content);
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800229 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800230}
231
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800232int CcnxWrapper::sendInterest (const string &strInterest, const DataCallback &dataCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800233{
234 ccn_charbuf *pname = ccn_charbuf_create();
235 ccn_closure *dataClosure = new ccn_closure;
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800236
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800237 ccn_name_from_uri (pname, strInterest.c_str());
238 dataClosure->data = new DataCallback (dataCallback); // should be removed when closure is removed
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800239
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800240 dataClosure->p = &incomingData;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800241 recursive_mutex::scoped_lock lock(m_mutex);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800242 if (ccn_express_interest (m_handle, pname, dataClosure, NULL) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800243 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("express interest failed"));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800244
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800245 ccn_charbuf_destroy (&pname);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800246}
247
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800248int CcnxWrapper::setInterestFilter (const string &prefix, const InterestCallback &interestCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800249{
250 ccn_charbuf *pname = ccn_charbuf_create();
251 ccn_closure *interestClosure = new ccn_closure;
252
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800253 ccn_name_from_uri (pname, prefix.c_str());
254 interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800255 interestClosure->p = &incomingInterest;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800256 if(ccn_set_interest_filter (m_handle, pname, interestClosure) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800257 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed"));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800258
259 ccn_charbuf_destroy(&pname);
260}
261
262}