blob: b901a436ed85d6f4961d4c817818722e623007c3 [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();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080048 ccn_disconnect (m_handle);
49 ccn_destroy (&m_handle);
50 ccn_charbuf_destroy (&m_keyLoactor);
51 ccn_keystore_destroy (&m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080052}
53
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080054/// @cond include_hidden
55
56void
57CcnxWrapper::createKeyLocator ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080058{
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080059 m_keyLoactor = ccn_charbuf_create();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080060 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG);
61 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_Key, CCN_DTAG);
62 int res = ccn_append_pubkey_blob (m_keyLoactor, ccn_keystore_public_key(m_keyStore));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080063 if (res >= 0)
Alexander Afanasyev1285b382012-03-08 16:40:27 -080064 {
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080065 ccn_charbuf_append_closer (m_keyLoactor); /* </Key> */
66 ccn_charbuf_append_closer (m_keyLoactor); /* </KeyLocator> */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080067 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080068}
69
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080070const ccn_pkey*
71CcnxWrapper::getPrivateKey ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080072{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080073 return ccn_keystore_private_key (m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080074}
75
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080076const unsigned char*
77CcnxWrapper::getPublicKeyDigest ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080078{
79 return ccn_keystore_public_key_digest(m_keyStore);
80}
81
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080082ssize_t
83CcnxWrapper::getPublicKeyDigestLength ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080084{
85 return ccn_keystore_public_key_digest_length(m_keyStore);
86}
87
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080088void
89CcnxWrapper::initKeyStore ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080090{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080091 ccn_charbuf *temp = ccn_charbuf_create ();
92 m_keyStore = ccn_keystore_create ();
93 ccn_charbuf_putf (temp, "%s/.ccnx/.ccnx_keystore", getenv("HOME"));
94 ccn_keystore_init (m_keyStore, ccn_charbuf_as_string(temp), (char*)"Th1s1sn0t8g00dp8ssw0rd.");
95 ccn_charbuf_destroy (&temp);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080096}
97
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080098void
99CcnxWrapper::ccnLoop ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800100{
101 pollfd pfds[1];
102 int res = ccn_run(m_handle, 0);
103
104 pfds[0].fd = ccn_get_connection_fd(m_handle);
105 pfds[0].events = POLLIN;
106
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800107 while (m_running)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800108 {
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800109 if (res >= 0)
110 {
111 int ret = poll(pfds, 1, 100);
112 if (ret >= 0)
113 {
114 recursive_mutex::scoped_lock lock(m_mutex);
115 res = ccn_run(m_handle, 0);
116 }
117 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800118 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800119}
120
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800121/// @endcond
122
123int
124CcnxWrapper::publishData (const string &name, const string &dataBuffer, int freshness)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800125{
126 ccn_charbuf *pname = ccn_charbuf_create();
127 ccn_charbuf *signed_info = ccn_charbuf_create();
128 ccn_charbuf *content = ccn_charbuf_create();
129
130 ccn_name_from_uri(pname, name.c_str());
131 ccn_signed_info_create(signed_info,
132 getPublicKeyDigest(),
133 getPublicKeyDigestLength(),
134 NULL,
135 CCN_CONTENT_DATA,
136 freshness,
137 NULL,
138 m_keyLoactor);
139 ccn_encode_ContentObject(content, pname, signed_info,
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800140 dataBuffer.c_str(), dataBuffer.length (),
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800141 NULL, getPrivateKey());
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800142 recursive_mutex::scoped_lock lock(m_mutex);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800143 ccn_put(m_handle, content->buf, content->length);
144
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800145 ccn_charbuf_destroy (&pname);
146 ccn_charbuf_destroy (&signed_info);
147 ccn_charbuf_destroy (&content);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800148}
149
150
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800151static ccn_upcall_res
152incomingInterest(ccn_closure *selfp,
153 ccn_upcall_kind kind,
154 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800155{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800156 CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800157
158 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800159 {
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800160 case CCN_UPCALL_FINAL:
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800161 delete f;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800162 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800163 return CCN_UPCALL_RESULT_OK;
164
165 case CCN_UPCALL_INTEREST:
166 break;
167
168 default:
169 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800170 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800171
Chaoyi Bian93d43102012-03-07 14:28:56 -0800172 string interest;
Chaoyi Bian02dba3c2012-03-07 21:45:22 -0800173 for (int i = 0; i < info->interest_comps->n - 1; i++)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800174 {
175 char *comp;
176 size_t size;
177 interest += "/";
178 ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size);
179 interest += comp;
180 }
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800181 (*f) (interest);
Chaoyi Bian93d43102012-03-07 14:28:56 -0800182 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800183}
184
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800185static ccn_upcall_res
186incomingData(ccn_closure *selfp,
187 ccn_upcall_kind kind,
188 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800189{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800190 CcnxWrapper::DataCallback *f = static_cast<CcnxWrapper::DataCallback*> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800191
192 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800193 {
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800194 case CCN_UPCALL_FINAL:
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800195 delete f; // this never called in unit tests...
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800196 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800197 return CCN_UPCALL_RESULT_OK;
198
199 case CCN_UPCALL_CONTENT:
200 break;
201
202 default:
203 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800204 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800205
206 char *pcontent;
207 size_t len;
208 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 -0800209 string name;
210 for (int i = 0; i < info->content_comps->n - 1; i++)
211 {
212 char *comp;
213 size_t size;
214 name += "/";
215 ccn_name_comp_get(info->content_ccnb, info->content_comps, i, (const unsigned char **)&comp, &size);
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800216 name += comp; // this will also crash if name doesn't have \0 ending
Chaoyi Bian4194b742012-03-08 17:21:35 -0800217 }
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800218 // this will crash when content doesn't have \0 ending
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800219 (*f) (name, (string)pcontent);
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800220 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800221}
222
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800223int CcnxWrapper::sendInterest (const string &strInterest, const DataCallback &dataCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800224{
225 ccn_charbuf *pname = ccn_charbuf_create();
226 ccn_closure *dataClosure = new ccn_closure;
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800227
228 ccn_name_from_uri (pname, strInterest.c_str());
229 dataClosure->data = new DataCallback (dataCallback); // should be removed when closure is removed
230
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800231 dataClosure->p = &incomingData;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800232 recursive_mutex::scoped_lock lock(m_mutex);
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800233 ccn_express_interest (m_handle, pname, dataClosure, NULL);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800234
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800235 ccn_charbuf_destroy (&pname);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800236}
237
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800238int CcnxWrapper::setInterestFilter (const string &prefix, const InterestCallback &interestCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800239{
240 ccn_charbuf *pname = ccn_charbuf_create();
241 ccn_closure *interestClosure = new ccn_closure;
242
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800243 ccn_name_from_uri (pname, prefix.c_str());
244 interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800245 interestClosure->p = &incomingInterest;
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800246 ccn_set_interest_filter (m_handle, pname, interestClosure);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800247
248 ccn_charbuf_destroy(&pname);
249}
250
251}