blob: 1b0e5b5622bd6eab3669953f4aa86fd3b2ac660d [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{
40 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 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 m_keyStore = ccn_keystore_create ();
Chaoyi Bian95a58c32012-03-09 15:43:59 -080096 string keyStoreFile = string(getenv("HOME")) + string("/.ccnx/.ccnx_keystore");
97 if (ccn_keystore_init (m_keyStore, (char *)keyStoreFile.c_str(), (char*)"Th1s1sn0t8g00dp8ssw0rd.") < 0)
98 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str(keyStoreFile.c_str()));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080099}
100
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800101void
102CcnxWrapper::ccnLoop ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800103{
104 pollfd pfds[1];
105 int res = ccn_run(m_handle, 0);
106
107 pfds[0].fd = ccn_get_connection_fd(m_handle);
108 pfds[0].events = POLLIN;
109
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800110 while (m_running)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800111 {
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800112 if (res >= 0)
113 {
114 int ret = poll(pfds, 1, 100);
115 if (ret >= 0)
116 {
117 recursive_mutex::scoped_lock lock(m_mutex);
118 res = ccn_run(m_handle, 0);
119 }
120 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800121 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800122}
123
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800124/// @endcond
125
126int
127CcnxWrapper::publishData (const string &name, const string &dataBuffer, int freshness)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800128{
129 ccn_charbuf *pname = ccn_charbuf_create();
130 ccn_charbuf *signed_info = ccn_charbuf_create();
131 ccn_charbuf *content = ccn_charbuf_create();
132
133 ccn_name_from_uri(pname, name.c_str());
134 ccn_signed_info_create(signed_info,
135 getPublicKeyDigest(),
136 getPublicKeyDigestLength(),
137 NULL,
138 CCN_CONTENT_DATA,
139 freshness,
140 NULL,
141 m_keyLoactor);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800142 if(ccn_encode_ContentObject(content, pname, signed_info,
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800143 dataBuffer.c_str(), dataBuffer.length (),
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800144 NULL, getPrivateKey()) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800145 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("encode content failed"));
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800146
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800147 recursive_mutex::scoped_lock lock(m_mutex);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800148 if (ccn_put(m_handle, content->buf, content->length) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800149 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800150
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800151 ccn_charbuf_destroy (&pname);
152 ccn_charbuf_destroy (&signed_info);
153 ccn_charbuf_destroy (&content);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800154}
155
156
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800157static ccn_upcall_res
158incomingInterest(ccn_closure *selfp,
159 ccn_upcall_kind kind,
160 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800161{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800162 CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800163
164 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800165 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800166 case CCN_UPCALL_FINAL: // effective in unit tests
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800167 delete f;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800168 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800169 return CCN_UPCALL_RESULT_OK;
170
171 case CCN_UPCALL_INTEREST:
172 break;
173
174 default:
175 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800176 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800177
Chaoyi Bian93d43102012-03-07 14:28:56 -0800178 string interest;
Chaoyi Bian02dba3c2012-03-07 21:45:22 -0800179 for (int i = 0; i < info->interest_comps->n - 1; i++)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800180 {
181 char *comp;
182 size_t size;
183 interest += "/";
184 ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800185 string compStr(comp, size);
186 interest += compStr;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800187 }
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800188 (*f) (interest);
Chaoyi Bian93d43102012-03-07 14:28:56 -0800189 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800190}
191
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800192static ccn_upcall_res
193incomingData(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::DataCallback *f = static_cast<CcnxWrapper::DataCallback*> (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: // effecitve in unit tests
Chaoyi Bian95a58c32012-03-09 15:43:59 -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_CONTENT:
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
213 char *pcontent;
214 size_t len;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800215 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 -0800216 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
217 string content(pcontent, len);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800218
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);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800226 string compStr(comp, size);
227 name += compStr;
Chaoyi Bian4194b742012-03-08 17:21:35 -0800228 }
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800229 (*f) (name, content);
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;
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800237
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800238 ccn_name_from_uri (pname, strInterest.c_str());
239 dataClosure->data = new DataCallback (dataCallback); // should be removed when closure is removed
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800240
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)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800244 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;
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700257 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
258 if (ret < 0)
259 {
260 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret));
261 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800262
263 ccn_charbuf_destroy(&pname);
264}
265
266}