blob: a4aea2e21b8395a033be2bcf9b0ca5dad24b80cd [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{
56 int res;
57
58 m_keyLoactor = ccn_charbuf_create();
59 ccn_charbuf_append_tt(m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG);
60 ccn_charbuf_append_tt(m_keyLoactor, CCN_DTAG_Key, CCN_DTAG);
61 res = ccn_append_pubkey_blob(m_keyLoactor, ccn_keystore_public_key(m_keyStore));
62 if (res >= 0)
Alexander Afanasyev1285b382012-03-08 16:40:27 -080063 {
64 ccn_charbuf_append_closer(m_keyLoactor); /* </Key> */
65 ccn_charbuf_append_closer(m_keyLoactor); /* </KeyLocator> */
66 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080067}
68
69const ccn_pkey* CcnxWrapper::getPrivateKey()
70{
71 return ccn_keystore_private_key(m_keyStore);
72}
73
74const unsigned char* CcnxWrapper::getPublicKeyDigest()
75{
76 return ccn_keystore_public_key_digest(m_keyStore);
77}
78
79ssize_t CcnxWrapper::getPublicKeyDigestLength()
80{
81 return ccn_keystore_public_key_digest_length(m_keyStore);
82}
83
84void CcnxWrapper::initKeyStore()
85{
86 ccn_charbuf *temp = ccn_charbuf_create();
87 m_keyStore = ccn_keystore_create();
88 ccn_charbuf_putf(temp, "%s/.ccnx/.ccnx_keystore", getenv("HOME"));
89 ccn_keystore_init(m_keyStore, ccn_charbuf_as_string(temp), (char*)"Th1s1sn0t8g00dp8ssw0rd.");
90 ccn_charbuf_destroy(&temp);
91}
92
93void CcnxWrapper::ccnLoop()
94{
95 pollfd pfds[1];
96 int res = ccn_run(m_handle, 0);
97
98 pfds[0].fd = ccn_get_connection_fd(m_handle);
99 pfds[0].events = POLLIN;
100
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800101 while (m_running)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800102 {
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800103 if (res >= 0)
104 {
105 int ret = poll(pfds, 1, 100);
106 if (ret >= 0)
107 {
108 recursive_mutex::scoped_lock lock(m_mutex);
109 res = ccn_run(m_handle, 0);
110 }
111 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800112 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800113}
114
Chaoyi Bian93d43102012-03-07 14:28:56 -0800115int CcnxWrapper::publishData(string name, string dataBuffer, int freshness)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800116{
117 ccn_charbuf *pname = ccn_charbuf_create();
118 ccn_charbuf *signed_info = ccn_charbuf_create();
119 ccn_charbuf *content = ccn_charbuf_create();
120
121 ccn_name_from_uri(pname, name.c_str());
122 ccn_signed_info_create(signed_info,
123 getPublicKeyDigest(),
124 getPublicKeyDigestLength(),
125 NULL,
126 CCN_CONTENT_DATA,
127 freshness,
128 NULL,
129 m_keyLoactor);
130 ccn_encode_ContentObject(content, pname, signed_info,
Chaoyi Bian93d43102012-03-07 14:28:56 -0800131 dataBuffer.c_str(), dataBuffer.length(),
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800132 NULL, getPrivateKey());
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800133 recursive_mutex::scoped_lock lock(m_mutex);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800134 ccn_put(m_handle, content->buf, content->length);
135
136 ccn_charbuf_destroy(&pname);
137 ccn_charbuf_destroy(&signed_info);
138 ccn_charbuf_destroy(&content);
139}
140
141
142static ccn_upcall_res incomingInterest(
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800143 ccn_closure *selfp,
144 ccn_upcall_kind kind,
145 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800146{
Chaoyi Bian49d46752012-03-07 10:35:21 -0800147 function<void (string)> f = *(function<void (string)> *)selfp->data;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800148
149 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800150 {
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800151 case CCN_UPCALL_FINAL:
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800152 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800153 return CCN_UPCALL_RESULT_OK;
154
155 case CCN_UPCALL_INTEREST:
156 break;
157
158 default:
159 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800160 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800161
Chaoyi Bian93d43102012-03-07 14:28:56 -0800162 string interest;
Chaoyi Bian02dba3c2012-03-07 21:45:22 -0800163 for (int i = 0; i < info->interest_comps->n - 1; i++)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800164 {
165 char *comp;
166 size_t size;
167 interest += "/";
168 ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size);
169 interest += comp;
170 }
Chaoyi Bian93d43102012-03-07 14:28:56 -0800171 f(interest);
172 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800173}
174
175static ccn_upcall_res incomingData(
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800176 ccn_closure *selfp,
177 ccn_upcall_kind kind,
178 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800179{
Chaoyi Bian4194b742012-03-08 17:21:35 -0800180 function<void (string, string)> f = *(function<void (string, string)> *)selfp->data;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800181
182 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800183 {
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800184 case CCN_UPCALL_FINAL:
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800185 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800186 return CCN_UPCALL_RESULT_OK;
187
188 case CCN_UPCALL_CONTENT:
189 break;
190
191 default:
192 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800193 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800194
195 char *pcontent;
196 size_t len;
197 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 -0800198 string name;
199 for (int i = 0; i < info->content_comps->n - 1; i++)
200 {
201 char *comp;
202 size_t size;
203 name += "/";
204 ccn_name_comp_get(info->content_ccnb, info->content_comps, i, (const unsigned char **)&comp, &size);
205 name += comp;
206 }
207 f(name, (string)pcontent);
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800208 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800209}
210
Chaoyi Bian4194b742012-03-08 17:21:35 -0800211int CcnxWrapper::sendInterest(string strInterest, function<void (string, string)> dataCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800212{
213 ccn_charbuf *pname = ccn_charbuf_create();
214 ccn_closure *dataClosure = new ccn_closure;
Chaoyi Bian4194b742012-03-08 17:21:35 -0800215 function<void (string, string)> *f = new function<void (string, string)>(dataCallback);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800216
217 ccn_name_from_uri(pname, strInterest.c_str());
Chaoyi Bian49d46752012-03-07 10:35:21 -0800218 dataClosure->data = f;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800219 dataClosure->p = &incomingData;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800220 recursive_mutex::scoped_lock lock(m_mutex);
221 ccn_express_interest(m_handle, pname, dataClosure, NULL);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800222
223 ccn_charbuf_destroy(&pname);
224}
225
Chaoyi Bian93d43102012-03-07 14:28:56 -0800226int CcnxWrapper::setInterestFilter(string prefix, function<void (string)> interestCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800227{
228 ccn_charbuf *pname = ccn_charbuf_create();
229 ccn_closure *interestClosure = new ccn_closure;
Chaoyi Bian49d46752012-03-07 10:35:21 -0800230 function<void (string)> *f = new function<void (string)>(interestCallback);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800231
232 ccn_name_from_uri(pname, prefix.c_str());
Chaoyi Bian49d46752012-03-07 10:35:21 -0800233 interestClosure->data = f;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800234 interestClosure->p = &incomingInterest;
235 ccn_set_interest_filter(m_handle, pname, interestClosure);
236
237 ccn_charbuf_destroy(&pname);
238}
239
240}