blob: a7936a323840753e3a0cca4a1ca09ef452f70dce [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()
32{
33 m_handle = ccn_create();
34 ccn_connect(m_handle, NULL);
35 initKeyStore();
36 createKeyLocator();
Chaoyi Bian49d46752012-03-07 10:35:21 -080037 m_thread = thread(&CcnxWrapper::ccnLoop, this);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080038}
39
40CcnxWrapper::~CcnxWrapper()
41{
42 running = false;
43 m_thread.join();
44 ccn_disconnect(m_handle);
45 ccn_destroy(&m_handle);
46 ccn_charbuf_destroy(&m_keyLoactor);
47 ccn_keystore_destroy(&m_keyStore);
48}
49
50void CcnxWrapper::createKeyLocator()
51{
52 int res;
53
54 m_keyLoactor = ccn_charbuf_create();
55 ccn_charbuf_append_tt(m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG);
56 ccn_charbuf_append_tt(m_keyLoactor, CCN_DTAG_Key, CCN_DTAG);
57 res = ccn_append_pubkey_blob(m_keyLoactor, ccn_keystore_public_key(m_keyStore));
58 if (res >= 0)
59 {
60 ccn_charbuf_append_closer(m_keyLoactor); /* </Key> */
61 ccn_charbuf_append_closer(m_keyLoactor); /* </KeyLocator> */
62 }
63}
64
65const ccn_pkey* CcnxWrapper::getPrivateKey()
66{
67 return ccn_keystore_private_key(m_keyStore);
68}
69
70const unsigned char* CcnxWrapper::getPublicKeyDigest()
71{
72 return ccn_keystore_public_key_digest(m_keyStore);
73}
74
75ssize_t CcnxWrapper::getPublicKeyDigestLength()
76{
77 return ccn_keystore_public_key_digest_length(m_keyStore);
78}
79
80void CcnxWrapper::initKeyStore()
81{
82 ccn_charbuf *temp = ccn_charbuf_create();
83 m_keyStore = ccn_keystore_create();
84 ccn_charbuf_putf(temp, "%s/.ccnx/.ccnx_keystore", getenv("HOME"));
85 ccn_keystore_init(m_keyStore, ccn_charbuf_as_string(temp), (char*)"Th1s1sn0t8g00dp8ssw0rd.");
86 ccn_charbuf_destroy(&temp);
87}
88
89void CcnxWrapper::ccnLoop()
90{
91 pollfd pfds[1];
92 int res = ccn_run(m_handle, 0);
93
94 pfds[0].fd = ccn_get_connection_fd(m_handle);
95 pfds[0].events = POLLIN;
96
97 while (running)
98 {
99 if (res >= 0)
100 {
101 int ret = poll(pfds, 1, 100);
102 if (ret >= 0)
103 {
104 boost::recursive_mutex::scoped_lock lock(m_mutex);
105 res = ccn_run(m_handle, 0);
106 }
107 }
108 }
109}
110
111int CcnxWrapper::publishData(string name, shared_ptr< DataBuffer > dataBuffer, int freshness)
112{
113 ccn_charbuf *pname = ccn_charbuf_create();
114 ccn_charbuf *signed_info = ccn_charbuf_create();
115 ccn_charbuf *content = ccn_charbuf_create();
116
117 ccn_name_from_uri(pname, name.c_str());
118 ccn_signed_info_create(signed_info,
119 getPublicKeyDigest(),
120 getPublicKeyDigestLength(),
121 NULL,
122 CCN_CONTENT_DATA,
123 freshness,
124 NULL,
125 m_keyLoactor);
126 ccn_encode_ContentObject(content, pname, signed_info,
127 dataBuffer->buffer(), dataBuffer->length(),
128 NULL, getPrivateKey());
129 ccn_put(m_handle, content->buf, content->length);
130
131 ccn_charbuf_destroy(&pname);
132 ccn_charbuf_destroy(&signed_info);
133 ccn_charbuf_destroy(&content);
134}
135
136
137static ccn_upcall_res incomingInterest(
138 ccn_closure *selfp,
139 ccn_upcall_kind kind,
140 ccn_upcall_info *info)
141{
Chaoyi Bian49d46752012-03-07 10:35:21 -0800142 function<void (string)> f = *(function<void (string)> *)selfp->data;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800143
144 switch (kind)
145 {
146 case CCN_UPCALL_FINAL:
147 free(selfp);
148 return CCN_UPCALL_RESULT_OK;
149
150 case CCN_UPCALL_INTEREST:
151 break;
152
153 default:
154 return CCN_UPCALL_RESULT_OK;
155 }
156
157 char *comp;
158 size_t size;
159 ccn_name_comp_get(info->content_ccnb, info->content_comps, info->content_comps->n - 3, (const unsigned char **)&comp, &size);
Chaoyi Bian49d46752012-03-07 10:35:21 -0800160 f((string)comp);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800161}
162
163static ccn_upcall_res incomingData(
164 ccn_closure *selfp,
165 ccn_upcall_kind kind,
166 ccn_upcall_info *info)
167{
Chaoyi Bian49d46752012-03-07 10:35:21 -0800168 function<void (shared_ptr<DataBuffer>)> f = *(function<void (shared_ptr<DataBuffer>)> *)selfp->data;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800169
170 switch (kind)
171 {
172 case CCN_UPCALL_FINAL:
173 free(selfp);
174 return CCN_UPCALL_RESULT_OK;
175
176 case CCN_UPCALL_CONTENT:
177 break;
178
179 default:
180 return CCN_UPCALL_RESULT_OK;
181 }
182
183 char *pcontent;
184 size_t len;
185 ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, (const unsigned char **)&pcontent, &len);
Chaoyi Bian49d46752012-03-07 10:35:21 -0800186 shared_ptr<DataBuffer> data(new DataBufferImpl((const unsigned char*)pcontent, len));
187 f(data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800188}
189
Chaoyi Bian49d46752012-03-07 10:35:21 -0800190int CcnxWrapper::sendInterest(string strInterest, function< void (shared_ptr< DataBuffer >) > dataCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800191{
192 ccn_charbuf *pname = ccn_charbuf_create();
193 ccn_closure *dataClosure = new ccn_closure;
Chaoyi Bian49d46752012-03-07 10:35:21 -0800194 function<void (shared_ptr< DataBuffer >)> *f = new function<void (shared_ptr< DataBuffer >)>(dataCallback);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800195
196 ccn_name_from_uri(pname, strInterest.c_str());
197 ccn_express_interest(m_handle, pname, dataClosure, NULL);
Chaoyi Bian49d46752012-03-07 10:35:21 -0800198 dataClosure->data = f;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800199 dataClosure->p = &incomingData;
200
201 ccn_charbuf_destroy(&pname);
202}
203
Chaoyi Bian49d46752012-03-07 10:35:21 -0800204int CcnxWrapper::setInterestFilter(string prefix, function< void (string) > interestCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800205{
206 ccn_charbuf *pname = ccn_charbuf_create();
207 ccn_closure *interestClosure = new ccn_closure;
Chaoyi Bian49d46752012-03-07 10:35:21 -0800208 function<void (string)> *f = new function<void (string)>(interestCallback);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800209
210 ccn_name_from_uri(pname, prefix.c_str());
Chaoyi Bian49d46752012-03-07 10:35:21 -0800211 interestClosure->data = f;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800212 interestClosure->p = &incomingInterest;
213 ccn_set_interest_filter(m_handle, pname, interestClosure);
214
215 ccn_charbuf_destroy(&pname);
216}
217
218}