blob: 0acde7f95fe76433d510a8ba53b29e236fa204c6 [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>
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080025
26using namespace std;
27using namespace boost;
28
29namespace Sync {
30
31CcnxWrapper::CcnxWrapper()
Alexander Afanasyev1285b382012-03-08 16:40:27 -080032 : m_handle (0)
33 , m_keyStore (0)
34 , m_keyLoactor (0)
35 , m_running (true)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080036{
37 m_handle = ccn_create();
38 ccn_connect(m_handle, NULL);
39 initKeyStore();
40 createKeyLocator();
Alexander Afanasyev1285b382012-03-08 16:40:27 -080041 m_thread = thread (&CcnxWrapper::ccnLoop, this);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080042}
43
44CcnxWrapper::~CcnxWrapper()
45{
Alexander Afanasyev1285b382012-03-08 16:40:27 -080046 m_running = false;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080047 m_thread.join();
48 ccn_disconnect(m_handle);
49 ccn_destroy(&m_handle);
50 ccn_charbuf_destroy(&m_keyLoactor);
51 ccn_keystore_destroy(&m_keyStore);
52}
53
54void CcnxWrapper::createKeyLocator()
55{
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080056 m_keyLoactor = ccn_charbuf_create();
57 ccn_charbuf_append_tt(m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG);
58 ccn_charbuf_append_tt(m_keyLoactor, CCN_DTAG_Key, CCN_DTAG);
Alexander Afanasyevc1030192012-03-08 22:21:28 -080059 int res = ccn_append_pubkey_blob(m_keyLoactor, ccn_keystore_public_key(m_keyStore));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080060 if (res >= 0)
Alexander Afanasyev1285b382012-03-08 16:40:27 -080061 {
62 ccn_charbuf_append_closer(m_keyLoactor); /* </Key> */
63 ccn_charbuf_append_closer(m_keyLoactor); /* </KeyLocator> */
64 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080065}
66
67const ccn_pkey* CcnxWrapper::getPrivateKey()
68{
69 return ccn_keystore_private_key(m_keyStore);
70}
71
72const unsigned char* CcnxWrapper::getPublicKeyDigest()
73{
74 return ccn_keystore_public_key_digest(m_keyStore);
75}
76
77ssize_t CcnxWrapper::getPublicKeyDigestLength()
78{
79 return ccn_keystore_public_key_digest_length(m_keyStore);
80}
81
82void CcnxWrapper::initKeyStore()
83{
84 ccn_charbuf *temp = ccn_charbuf_create();
85 m_keyStore = ccn_keystore_create();
86 ccn_charbuf_putf(temp, "%s/.ccnx/.ccnx_keystore", getenv("HOME"));
87 ccn_keystore_init(m_keyStore, ccn_charbuf_as_string(temp), (char*)"Th1s1sn0t8g00dp8ssw0rd.");
88 ccn_charbuf_destroy(&temp);
89}
90
91void CcnxWrapper::ccnLoop()
92{
93 pollfd pfds[1];
94 int res = ccn_run(m_handle, 0);
95
96 pfds[0].fd = ccn_get_connection_fd(m_handle);
97 pfds[0].events = POLLIN;
98
Alexander Afanasyev1285b382012-03-08 16:40:27 -080099 while (m_running)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800100 {
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800101 if (res >= 0)
102 {
103 int ret = poll(pfds, 1, 100);
104 if (ret >= 0)
105 {
106 recursive_mutex::scoped_lock lock(m_mutex);
107 res = ccn_run(m_handle, 0);
108 }
109 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800110 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800111}
112
Chaoyi Bian93d43102012-03-07 14:28:56 -0800113int CcnxWrapper::publishData(string name, string dataBuffer, int freshness)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800114{
115 ccn_charbuf *pname = ccn_charbuf_create();
116 ccn_charbuf *signed_info = ccn_charbuf_create();
117 ccn_charbuf *content = ccn_charbuf_create();
118
119 ccn_name_from_uri(pname, name.c_str());
120 ccn_signed_info_create(signed_info,
121 getPublicKeyDigest(),
122 getPublicKeyDigestLength(),
123 NULL,
124 CCN_CONTENT_DATA,
125 freshness,
126 NULL,
127 m_keyLoactor);
128 ccn_encode_ContentObject(content, pname, signed_info,
Chaoyi Bian93d43102012-03-07 14:28:56 -0800129 dataBuffer.c_str(), dataBuffer.length(),
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800130 NULL, getPrivateKey());
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800131 recursive_mutex::scoped_lock lock(m_mutex);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800132 ccn_put(m_handle, content->buf, content->length);
133
134 ccn_charbuf_destroy(&pname);
135 ccn_charbuf_destroy(&signed_info);
136 ccn_charbuf_destroy(&content);
137}
138
139
140static ccn_upcall_res incomingInterest(
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800141 ccn_closure *selfp,
142 ccn_upcall_kind kind,
143 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800144{
Chaoyi Bian49d46752012-03-07 10:35:21 -0800145 function<void (string)> f = *(function<void (string)> *)selfp->data;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800146
147 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800148 {
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800149 case CCN_UPCALL_FINAL:
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800150 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800151 return CCN_UPCALL_RESULT_OK;
152
153 case CCN_UPCALL_INTEREST:
154 break;
155
156 default:
157 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800158 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800159
Chaoyi Bian93d43102012-03-07 14:28:56 -0800160 string interest;
Chaoyi Bian02dba3c2012-03-07 21:45:22 -0800161 for (int i = 0; i < info->interest_comps->n - 1; i++)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800162 {
163 char *comp;
164 size_t size;
165 interest += "/";
166 ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size);
167 interest += comp;
168 }
Chaoyi Bian93d43102012-03-07 14:28:56 -0800169 f(interest);
170 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800171}
172
173static ccn_upcall_res incomingData(
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800174 ccn_closure *selfp,
175 ccn_upcall_kind kind,
176 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800177{
Chaoyi Bian4194b742012-03-08 17:21:35 -0800178 function<void (string, string)> f = *(function<void (string, string)> *)selfp->data;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800179
180 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800181 {
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800182 case CCN_UPCALL_FINAL:
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800183 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800184 return CCN_UPCALL_RESULT_OK;
185
186 case CCN_UPCALL_CONTENT:
187 break;
188
189 default:
190 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800191 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800192
193 char *pcontent;
194 size_t len;
195 ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, (const unsigned char **)&pcontent, &len);
Chaoyi Bian4194b742012-03-08 17:21:35 -0800196 string name;
197 for (int i = 0; i < info->content_comps->n - 1; i++)
198 {
199 char *comp;
200 size_t size;
201 name += "/";
202 ccn_name_comp_get(info->content_ccnb, info->content_comps, i, (const unsigned char **)&comp, &size);
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800203 name += comp; // this will also crash if name doesn't have \0 ending
Chaoyi Bian4194b742012-03-08 17:21:35 -0800204 }
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800205 // this will crash when content doesn't have \0 ending
Chaoyi Bian4194b742012-03-08 17:21:35 -0800206 f(name, (string)pcontent);
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800207 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800208}
209
Chaoyi Bian4194b742012-03-08 17:21:35 -0800210int CcnxWrapper::sendInterest(string strInterest, function<void (string, string)> dataCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800211{
212 ccn_charbuf *pname = ccn_charbuf_create();
213 ccn_closure *dataClosure = new ccn_closure;
Chaoyi Bian4194b742012-03-08 17:21:35 -0800214 function<void (string, string)> *f = new function<void (string, string)>(dataCallback);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800215
216 ccn_name_from_uri(pname, strInterest.c_str());
Chaoyi Bian49d46752012-03-07 10:35:21 -0800217 dataClosure->data = f;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800218 dataClosure->p = &incomingData;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800219 recursive_mutex::scoped_lock lock(m_mutex);
220 ccn_express_interest(m_handle, pname, dataClosure, NULL);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800221
222 ccn_charbuf_destroy(&pname);
223}
224
Chaoyi Bian93d43102012-03-07 14:28:56 -0800225int CcnxWrapper::setInterestFilter(string prefix, function<void (string)> interestCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800226{
227 ccn_charbuf *pname = ccn_charbuf_create();
228 ccn_closure *interestClosure = new ccn_closure;
Chaoyi Bian49d46752012-03-07 10:35:21 -0800229 function<void (string)> *f = new function<void (string)>(interestCallback);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800230
231 ccn_name_from_uri(pname, prefix.c_str());
Chaoyi Bian49d46752012-03-07 10:35:21 -0800232 interestClosure->data = f;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800233 interestClosure->p = &incomingInterest;
234 ccn_set_interest_filter(m_handle, pname, interestClosure);
235
236 ccn_charbuf_destroy(&pname);
237}
238
239}