blob: 0684cea56f25c028f4fe9b189f4b66fcf049f946 [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>
26typedef 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();
Zhenkai Zhu009ff792012-03-09 12:37:52 -080040 if (ccn_connect(m_handle, NULL) < 0)
41 BOOST_THROW_EXCEPTION (CcnxOperationException()
42 << 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 Afanasyev1285b382012-03-08 16:40:27 -080050 m_running = false;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080051 m_thread.join();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080052 ccn_disconnect (m_handle);
53 ccn_destroy (&m_handle);
54 ccn_charbuf_destroy (&m_keyLoactor);
55 ccn_keystore_destroy (&m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080056}
57
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080058/// @cond include_hidden
59
60void
61CcnxWrapper::createKeyLocator ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080062{
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080063 m_keyLoactor = ccn_charbuf_create();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080064 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG);
65 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_Key, CCN_DTAG);
66 int res = ccn_append_pubkey_blob (m_keyLoactor, ccn_keystore_public_key(m_keyStore));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080067 if (res >= 0)
Alexander Afanasyev1285b382012-03-08 16:40:27 -080068 {
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080069 ccn_charbuf_append_closer (m_keyLoactor); /* </Key> */
70 ccn_charbuf_append_closer (m_keyLoactor); /* </KeyLocator> */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080071 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080072}
73
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080074const ccn_pkey*
75CcnxWrapper::getPrivateKey ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080076{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080077 return ccn_keystore_private_key (m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080078}
79
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080080const unsigned char*
81CcnxWrapper::getPublicKeyDigest ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080082{
83 return ccn_keystore_public_key_digest(m_keyStore);
84}
85
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080086ssize_t
87CcnxWrapper::getPublicKeyDigestLength ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080088{
89 return ccn_keystore_public_key_digest_length(m_keyStore);
90}
91
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080092void
93CcnxWrapper::initKeyStore ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080094{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080095 ccn_charbuf *temp = ccn_charbuf_create ();
96 m_keyStore = ccn_keystore_create ();
Zhenkai Zhu009ff792012-03-09 12:37:52 -080097 string keyStoreFile = string(getenv("HOME")) + string("/.ccnx/.ccnx_keystore");
98 if (ccn_keystore_init (m_keyStore, (char *)keyStoreFile.c_str(), (char*)"Th1s1sn0t8g00dp8ssw0rd.") < 0)
99 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str(keyStoreFile.c_str()));
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800100 ccn_charbuf_destroy (&temp);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800101}
102
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800103void
104CcnxWrapper::ccnLoop ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800105{
106 pollfd pfds[1];
107 int res = ccn_run(m_handle, 0);
108
109 pfds[0].fd = ccn_get_connection_fd(m_handle);
110 pfds[0].events = POLLIN;
111
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800112 while (m_running)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800113 {
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800114 if (res >= 0)
115 {
116 int ret = poll(pfds, 1, 100);
117 if (ret >= 0)
118 {
119 recursive_mutex::scoped_lock lock(m_mutex);
120 res = ccn_run(m_handle, 0);
121 }
122 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800123 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800124}
125
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800126/// @endcond
127
128int
129CcnxWrapper::publishData (const string &name, const string &dataBuffer, int freshness)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800130{
131 ccn_charbuf *pname = ccn_charbuf_create();
132 ccn_charbuf *signed_info = ccn_charbuf_create();
133 ccn_charbuf *content = ccn_charbuf_create();
134
135 ccn_name_from_uri(pname, name.c_str());
136 ccn_signed_info_create(signed_info,
137 getPublicKeyDigest(),
138 getPublicKeyDigestLength(),
139 NULL,
140 CCN_CONTENT_DATA,
141 freshness,
142 NULL,
143 m_keyLoactor);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800144 if(ccn_encode_ContentObject(content, pname, signed_info,
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800145 dataBuffer.c_str(), dataBuffer.length (),
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800146 NULL, getPrivateKey()) < 0)
147 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("encode content failed"));
148
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800149 recursive_mutex::scoped_lock lock(m_mutex);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800150 if (ccn_put(m_handle, content->buf, content->length) < 0)
151 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800152
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800153 ccn_charbuf_destroy (&pname);
154 ccn_charbuf_destroy (&signed_info);
155 ccn_charbuf_destroy (&content);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800156}
157
158
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800159static ccn_upcall_res
160incomingInterest(ccn_closure *selfp,
161 ccn_upcall_kind kind,
162 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800163{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800164 CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800165
166 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800167 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800168 case CCN_UPCALL_FINAL: // effective in unit tests
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800169 delete f;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800170 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800171 return CCN_UPCALL_RESULT_OK;
172
173 case CCN_UPCALL_INTEREST:
174 break;
175
176 default:
177 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800178 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800179
Chaoyi Bian93d43102012-03-07 14:28:56 -0800180 string interest;
Chaoyi Bian02dba3c2012-03-07 21:45:22 -0800181 for (int i = 0; i < info->interest_comps->n - 1; i++)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800182 {
183 char *comp;
184 size_t size;
185 interest += "/";
186 ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size);
187 interest += comp;
188 }
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800189 (*f) (interest);
Chaoyi Bian93d43102012-03-07 14:28:56 -0800190 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800191}
192
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800193static ccn_upcall_res
194incomingData(ccn_closure *selfp,
195 ccn_upcall_kind kind,
196 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800197{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800198 CcnxWrapper::DataCallback *f = static_cast<CcnxWrapper::DataCallback*> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800199
200 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800201 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800202 case CCN_UPCALL_FINAL: // effecitve in unit tests
203 delete f;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800204 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800205 return CCN_UPCALL_RESULT_OK;
206
207 case CCN_UPCALL_CONTENT:
208 break;
209
210 default:
211 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800212 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800213
214 char *pcontent;
215 size_t len;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800216 if (ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, (const unsigned char **)&pcontent, &len) < 0)
217 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
218
Chaoyi Bian4194b742012-03-08 17:21:35 -0800219 string name;
220 for (int i = 0; i < info->content_comps->n - 1; i++)
221 {
222 char *comp;
223 size_t size;
224 name += "/";
225 ccn_name_comp_get(info->content_ccnb, info->content_comps, i, (const unsigned char **)&comp, &size);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800226 name += comp;
Chaoyi Bian4194b742012-03-08 17:21:35 -0800227 }
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800228 // this will crash when content doesn't have \0 ending
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800229 (*f) (name, (string)pcontent);
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800230 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800231}
232
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800233int CcnxWrapper::sendInterest (const string &strInterest, const DataCallback &dataCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800234{
235 ccn_charbuf *pname = ccn_charbuf_create();
236 ccn_closure *dataClosure = new ccn_closure;
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800237
238 ccn_name_from_uri (pname, strInterest.c_str());
239 dataClosure->data = new DataCallback (dataCallback); // should be removed when closure is removed
240
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800241 dataClosure->p = &incomingData;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800242 recursive_mutex::scoped_lock lock(m_mutex);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800243 if (ccn_express_interest (m_handle, pname, dataClosure, NULL) < 0)
244 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("express interest failed"));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800245
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800246 ccn_charbuf_destroy (&pname);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800247}
248
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800249int CcnxWrapper::setInterestFilter (const string &prefix, const InterestCallback &interestCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800250{
251 ccn_charbuf *pname = ccn_charbuf_create();
252 ccn_closure *interestClosure = new ccn_closure;
253
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800254 ccn_name_from_uri (pname, prefix.c_str());
255 interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800256 interestClosure->p = &incomingInterest;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800257 if(ccn_set_interest_filter (m_handle, pname, interestClosure) < 0)
258 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed"));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800259
260 ccn_charbuf_destroy(&pname);
261}
262
263}