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